Regardign strtok() output directing to 2-D string array

Hi,

I just wrote a program in C to split a comma seperated string in to group of strings using strtok() function. The code is:

int main()
{
    char *temp;//not used here but basically we extract one string after another using strtok() and assign to a string pointer defined like this.
    char *str="aa,bb,cc,dd";
    int count=0;
    for(int i=0;i<strlen(str);i++)
    {
      if(str==',')
      count++;
    }

    char *ss[count];//2-d array where i want to store splitted results
    int j=0;
    *(ss+j)=strtok(str,",");

    while(*(ss+j)!=NULL)
    {
      *(ss+j)=strtok(NULL,",");
      j++;
    }
    
    //print  output
    for(int i=0;i<strlen(*ss);i++)
    printf("%s\t",*(ss+i));

    return 0;

}

The program compiles without error(please bear any syntax error, since i typed in hand without the code with me but the orignial program compiles successfully) but i am getting runtime error like 'Segmentation Fault'. I hope there needs to be proper memory handling here which i am not sure of.

Please help me in getting this splitted set of strings to a new 2-D array.

Thanks in advance.

Hi,
Just a little comment after a quick look to your code: i would suggest you to allocate memory properly (using malloc) and not use a shortcut like this

for(int i=0;i<strlen(str);i++)
    {
      if(str==',')
      count++;
    }

    //char *ss[count];//2-d array where i want to store splitted results
    ss = malloc(count*sizeof(char)); // try this one!

Thanks a ton.

This is working fine now....

No problem, glad i could help :smiley: