C control flow, memory allocation: Abort trap 6, populate array with characters in string, nested for loops -


this question control flow , possibly memory allocation.

given long long number, doing following:

  • converting string
  • iterating through characters of string in prescribed pattern (first layer of for loop)
  • performing operation on each selected character (second layer of for loop)
  • populating array processed data
#include <stdio.h> #include <string.h>  int main(void) {     long long n = 12345678; // given number      char str[8]; // initialize string of length 8     sprintf(str, "%2lld", n); // convert n string     printf("the string is: %s\n", str); // check n converted string      int arr[4]; // initialize array of length 4      (int = 6; >= 0; -= 2) // select every other char in string, starting second-to-last char         {             (int j = 0; j < 4; j++) // select position of array             {                        arr[j] = (str[i] - '0') * 2; // convert char int, multiply 2, , assign array position                 printf("the digit %c , product %d\n", str[i], arr[j]); // announce each entry array             }         }      (int k = 0; k < 4; k++) // print contents of array     {         printf("the product @ position %d %d\n", k, arr[k]);     }     return 0; } 

there 2 problems code:

  1. generates abort trap: 6 error when run on macos terminal.
  2. when code executes in practice ide environment, generates following result:

the string is: 12345678 digit 7 , product 14 digit 7 , product 14 digit 7 , product 14 digit 7 , product 14 digit 5 , product 10 digit 5 , product 10 digit 5 , product 10 digit 5 , product 10 digit 3 , product 6 digit 3 , product 6 digit 3 , product 6 digit 3 , product 6 digit 1 , product 2 digit 1 , product 2 digit 1 , product 2 digit 1 , product 2 product @ position 0 2 product @ position 1 2 product @ position 2 2 product @ position 3 2 

what want resolve abort trap: 6 error , following result:

the string is: 12345678 digit 7 , product 14 digit 5 , product 10 digit 3 , product 6 digit 1 , product 2 product @ position 0 14 product @ position 1 10 product @ position 2 6 product @ position 3 2 

what should change in code achieve that?

i have read other posts abort trap: 6 , don't know how code committing mistake in memory allocation/usage.

you dont need for loop increment j array index, set 0 @ beginning , every loop increment it.

for (int = 6, j = 0; >= 0; -= 2, j++) // select every other char in string, starting second-to-last char {     arr[j] = (str[i] - '0') * 2; // convert char int, multiply 2, , assign array position     printf("the digit %c , product %d\n", str[i], arr[j]); // announce each entry array } 

as @keine lust mentioned in comments, have alloc space null terminating character.

char str[8]; 

change to

char str[9]; 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - VueJS2 and the Window Object - how to use? -