Reading to array of char *s

This is a discussion on Reading to array of char *s within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to parse a line of text from a file into an array of character pointers. I ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Unhappy Reading to array of char *s

    Hi,

    I am trying to parse a line of text from a file into an array of character pointers. I am failing.

    It is rather strange as I am parsing the text from the string, I can store the pointer and print to the screen. However, when I try to access the array of pointers after the parsing loop, I get an error.

    Code:
    	/* while there is more data in school file */
    	while( fgets(curSchoolLine, sizeof(curSchoolLine), school_csvHandle) != NULL ) 
    	{
    		char * schoolTokens[7];
    		int s = 0;
    
    		/* parse the current line into tokens by comma */
    		schoolTokens[s] = strtok(curSchoolLine, ",");
    		while(schoolTokens[s] != NULL)
    		{ 
    			printf("school token: %s\n", schoolTokens[s]);
    			schoolTokens[s] = strtok(NULL, ",");
    			
    		}  /* end parse the current line into tokens by comma */
    
    		printf("school token: %s\n", &schoolTokens[1])
    
    }
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Until you post more context or sample I/O, take a closer look at this line.
    Code:
    printf("school token: %s\n", &schoolTokens[1])
    The address of a pointer to a string is a pointer to a pointer to a char, which does not correspond to %s. A semicolon would be nice, too.

    [edit]Ooh; some fun debugging. If you intend to increment s for each string, you'll need to keep track of the potential for going out of bounds on the pointer array before going for the final strtok failure.
    Last edited by Dave_Sinkula; 04-28-2005 at 04:17 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Yeah, I just noticed both errors you just mentioned.

    Thanks!
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. reading int into 2-d char array
    By lambs4 in forum C Programming
    Replies: 7
    Last Post: 07-24-2002, 04:25 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21