Thread: Initializing char * array

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Question Initializing char * array

    I am reading lines from a file and parsing them into tokens. I am storing the tokens into a string array. Everything works fine until I leave the while loop and try to access the data. It does not seem to exist outside the while loop.

    Code followed by output:

    Code:
    char *src[15];
    
    while(fgets(line, 100, r) != NULL)
    {
         /*Skip strings starting with #*/
         if(line[0] != '#')
         {
              src[x] = (char *)strtok(line, delim);
              x++;
              fprintf(o, "inside while: %s\n", src[x-1]);
         }
    }
    
    for (k=0; k<15; k++)
    {
         fprintf(o, "outside while: %s\n", src[k]);
    }
    Here’s the ouput

    inside while: 192.0.32
    inside while: 192.0.34
    outside while:
    outside while:

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char *src[15];
    This is an array of character pointers. This does not actually allocate any memory for you to use. You have to allocate space first, then copy the data into it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >This does not actually allocate any memory for you to use.
    I don't see how this is relevant to parsing one line at a time. Since he is not copying memory, just pointers, there's no problem. Unless he wants to hold more than one line at a time memory allocation isn't really needed.

    >I am reading lines from a file and parsing them into tokens.
    Not quite, you're reading lines into an array and parsing only the first token of each line. If this is what you want then you need to copy what the pointers point to instead of just the pointers themselves, otherwise you need to change how you tokenize the line. Generally, if you want to parse a line at a time as you seem to want, you read the line, tokenize it completely, and then process it all in one loop iteration:
    Code:
    while ( fgets ( line, 100, r ) != NULL )
    {
      int i;
      int index = 0;
      char *sep;
    
      if ( line[0] == '#' ) 
        continue;
    
      sep = strtok ( line, delim );
      while ( sep != NULL ) {
        src[index++] = sep;
        sep = strtok ( NULL, delim );
      }
    
      for ( i = 0; i < index; i++ )
        fprintf ( o, "After tokenization: %s\n", src[i] );
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Angry Still not working

    Here is my latest code based on replies (thank you ). After exiting the while loop, the array still does not maintain the entries I copied into it.

    Code:
    while(fgets(line, Max, r) != NULL)
    {
       int i, j;
       int index = 0;
       int slot = 0;
       char *sep;
    	   
       if(line[0] == '#' || line[0] == '\n')
                continue;
                
       sep = strtok(line, delim);
       while (sep != NULL)
       {
              routes[index][slot++] = sep;
              sep = strtok (NULL, delim);
       }
       for (j=0; j<3; j++)
              fprintf(o, "After tokenization: %s\n", routes[index][j]);
       index++;
    }
    
    fprintf(o, "Outside while: %s\n", routes[0][0]);
    fprintf(o, "Outside while: %s\n", routes[0][1]);
    fprintf(o, "Outside while: %s\n", routes[0][2]);
    fprintf(o, "Outside while: %s\n", routes[0][0]);
    fprintf(o, "Outside while: %s\n", routes[0][1]);
    fprintf(o, "Outside while: %s\n", routes[0][2]);
    my output:


    After tokenization: 192.0.32
    After tokenization: 255.255.255
    After tokenization: 206.220.243.34
    After tokenization: default
    After tokenization: 206.220.243.60
    After tokenization: .60
    Outside while:
    Outside while: 206.220.243.60
    Outside while: .60
    Outside while:
    Outside while: 206.220.243.60
    Outside while: .60

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Red face Beware Java Programmers

    I fixed the problem by replacing this line of code:

    Code:
    routes[count][slot] =  sep;
    With this one:

    Code:
    strcpy(routes[count][slot], sep);
    C does not handle String assignments like Java due to the fact that in C, a String is really a pointer to an array of chars.

  6. #6
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    //while (sep != NULL)
    {
    routes[index][slot++] = sep;
    sep = strtok (NULL, delim);
    }
    //

    use your original ;
    char *src[15];

    while (sep != NULL)
    {
    strcpy(src[x], sep);
    x++;
    sep = strtok (NULL, delim);
    }

    EDIT;
    *oh i was a few minutes late , but im glad everything worked out

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You did not provide enough code to troubleshoot the issue. Most of your data types we were left to guess at.

    As I stated before: If you are copying data into a character pointer, it has to have memory allocated to it.

    You can make pointers point to different locations, but there is no garbage collection. You must implicitly free everything you allocate.

    Here is a working example of how strtok works based off the code example you gave us that had the problem:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
    	char buf[BUFSIZ] = {0};
    	char *ptrs[BUFSIZ] = {0};
    	char *ptr;
    	int x;
    
    	printf("Enter a tokenized string. Use '.' for the token.\n");
    	while( fgets( buf, BUFSIZ, stdin ) != NULL )
    	{
    		ptr = strtok( buf, "." );
    		x = 0;
    		while( ptr != NULL )
    		{
    			ptrs[x++] = ptr;
    			ptr = strtok( NULL, "." );
    		}
    		for( x = 0; x < BUFSIZ; x++ )
    			if( ptrs[x] )
    				printf("%s\t", ptrs[x] );
    	printf("\nEnter a tokenized string. Use '.' for the token.\n");
    	}
    	return 0;
    }
    strtok tokenizes the string, assigning pointers to each segment to the array.

    [edit]Wording change, token to segment in final sentence.[/edit]

    Quzah.
    Last edited by quzah; 03-11-2003 at 05:26 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM