-2

Can you please help me with fixing below code. Not sure where the segmentation fault is.

 char str[] = "00ab00,00cd00";
 char **strptr;
 int i;

 strptr = malloc(sizeof(char*) * 2);

 strcnt = 0;
 int j=0;
 for(i=0;i<sizeof(str);i++) {

   char c = *(str+i);
   printf("%c", c);

   if(c==',') {
     strcnt++;
     j=0;
   }

   strptr[strcnt][j++] = c;

 }

Please ignore my poor coding :)

PS: I know its possible to split using strtok() easily.

user691197
  • 927
  • 6
  • 20
  • 38
  • you're allocating the array or arrays but not each array. With strtok you would have to do that too. if you provide a [mcve] we may be able to fix your code. but not like that. – Jean-François Fabre Sep 24 '16 at 15:54
  • Do you mean something like this - strptr[strcnt] = malloc(sizeof(char*)); ? I did this but it still doesnt help. – user691197 Sep 24 '16 at 16:11
  • 1
    `strptr[strcnt] = malloc(real_string_size+1)` would be better; – Jean-François Fabre Sep 24 '16 at 16:13
  • 1
    @user691197 "help me with fixing below code" - how about you describe what this code is *supposed to be doing* ? And no, I don't mean the last two words of your question title. If your intent is to have your pointer array contain pointers *into* the `str` buffer, which you modify to replace `,` with `\0`, then you can do this only allocating the pointer array. Of course, the data each pointer points to is still in `str[]`, similar to how `strtok` would work. If that is what you're *trying* to do say so. If something different, *say so*, but don't just drop code and say "fix it please". – WhozCraig Sep 24 '16 at 16:16
  • 1
    http://ideone.com/hgmb0c – BLUEPIXY Sep 24 '16 at 17:25
  • @BLUEPIXY is your comment inappropriate because it's [an answer presented as a comment](http://meta.stackexchange.com/questions/163587/when-to-answer-vs-when-to-just-comment) or because it's [posted offsite instead of on SO](http://meta.stackoverflow.com/questions/284333/linking-to-ideone-to-show-your-code-works)? Just trying to understand proper SO etiquette. – cdlane Sep 24 '16 at 17:40
  • perhaps, OP want things different, but will be a hint. – BLUEPIXY Sep 24 '16 at 18:00
  • Side note: `sizeof(str)` is more than you think it is. Use `sizeof(str)-1` or `strlen(str)`. – barak manos Sep 24 '16 at 18:48

1 Answers1

0

Not sure where the segmentation fault is

As others have mentioned in the comments, you are not assigning memory to the pointers strptr[0] and strptr[1] but, you are trying to access them. This leads to segmentation fault.

Use a for loop to initially assign memory to strptr[0] and strptr[1]

strptr = malloc(sizeof(char*) * 2);
for(i = 0; i < 2; i++) //here, initialise each to 1 byte
{
    strptr[i] = malloc(1); 
}
strcnt = 0;

Here's a question on how to initialise a pointer to a pointer.


then, resize them at each step as you add additional character using the realloc() function.

for(i = 0, j = 0; i < sizeof(str); i++) 
{

   strptr[strcnt] = realloc(strptr[strcnt], j + 2); 
   //here, you resize each time to provide space for additional character using realloc() 
   char c = *(str + i);

   printf("%c", c);

   if(c == ',') 
   {
     ++strcnt;
     j=0;
     continue; //use a continue here
   }

   strptr[strcnt][j] = c;
   strptr[strcnt][++j] = '\0'; 
   //to provide null terminating character at the end of string (updated to next position at every iteration)
}

don't forget to free() the allocated memory

for( i=0; i<2; i++)
{
    printf("%s\n", strptr[i]); //just to display the string before `free`ing
    free(strptr[i]);
}

free(strptr);

Altogether your code would be something like this:

char str[] = "00ab00,00cd00";
char **strptr;

int i, j;
int strcnt; 

strptr = malloc(sizeof(char*) * 2);
for(i = 0; i < 2; i++)
{
    strptr[i] = malloc(1); 
}
strcnt = 0;


for(i = 0, j = 0; i < sizeof(str); i++) 
{

   strptr[strcnt] = realloc(strptr[strcnt], j + 2); 
   char c = *(str + i);

   printf("%c", c);

   if(c == ',') 
   {
     ++strcnt;
     j=0;
     continue;
   }

   strptr[strcnt][j] = c;
   strptr[strcnt][++j] = '\0';
}

for( i=0; i<2; i++)
{
    printf("%s\n", strptr[i]);
    free(strptr[i]);
}

free(strptr);

return 0;
Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37