Thread: can't assign proper values to an array of string

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    can't assign proper values to an array of string

    yeah. er. I shall explain! I am trying to write a function that parses through a text file which contains a word on each line, then returns an array of strings of a certain size (that is, all strings that contain, say, 4 chars are put into an array). to do this. I parse through the file twice, first time to determine how many strings are of that size. then I initialise a char ** to that size. second time, all strings of that size are given the appropriate value. right, all of this basically works fine. here's the weird thing : when I try to print out the values after they have been assigned to their separate values in the array, it prints out the correct number of values, it's just that they are all the very last string in the file, which usually isn't even the right size. however, if I print out the string that is supposed to be assigned to the array from within the if statement, it prints the correct one. it only seem to go wrong when I assign. I have no idea why this is. if that wasn't too clear, here is the cod :

    Code:
    void parselist (int length, FILE *list, char **parsedlist)
    {
    	char string[100];
    	int compare, sizeoflist = 0, i = 0;
    	char *word;
    
    	while (fgets(string, 100, list) != NULL)
    	{
    		if (length == strlen(string) )
    		{
    			sizeoflist++;
    		}
    	}
    
    	parsedlist = calloc(sizeoflist, sizeof(char) );
    	
    	fseek(list, 0, SEEK_SET);
    
    	while (fgets (string, 100, list) != NULL)
    	{
    		if (strlen(string) == length)
    		{
    			parsedlist[i] = string;
    			i++;
    		}
    	}
    
    	for (i = 0; i < sizeoflist; i++)
    	{
    		printf("%s", parsedlist[i]);
    	}
    }
    which does not work.

    Code:
    void parselist (int length, FILE *list, char **parsedlist)
    {
    	char string[100];
    	int sizeoflist = 0, i = 0;
    	char *word;
    
    	while (fgets(string, 100, list) != NULL)
    	{
    		if (length == strlen(string) )
    		{
    			sizeoflist++;
    		}
    	}
    
    	parsedlist = calloc(sizeoflist, sizeof(char) );
    	
    	fseek(list, 0, SEEK_SET);
    
    	while (fgets (string, 100, list) != NULL)
    	{
    		if (strlen(string) == length)
    		{
    			printf("%s", string);
    		}
    	}
    which prints out correct values.

    I suppose it's something obvious. any ideas? =/

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't you be allocating enough space in your strings for the null character? In case you're confused as to the answer, it's 'yes'. Then, shouldn't you be actually allocating space for each of those strings and using something like oh, strcpy to copy the contents of that string into that space you've allocated? Again, in case you're wondering, the answer is 'yes'.

    Oh, and while you're at it, you'll need to make sure you've allocated enough pointers to "strings" to hold these actual strings.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM