The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 04-27-2008
SankarV SankarV is offline
Registered User
 

Join Date: Feb 2006
Location: India
Posts: 13
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:

Code:
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[i]==',')
      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.
Reply With Quote
Forum Sponsor