Thread: Lists and Char Pointers - Last element overwriting all others

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    29

    Lists and Char Pointers - Last element overwriting all others

    Hi,

    I have one list that contains multiple smaller lists. Each smaller list contains char arrays (well that where things get messy with pointers etc).

    So the problem is that that contents of the smaller lists kept getting overwritten by the last element and I can’t understand why although I’m sure it’s something to with pointers and I have tried numerous different things including using strcpy. This is part of a large project and simulator so I’ve just included the relevant code below: I wrote a small for loop at the end to just print out the contents of the list to check that it was over writing.

    I’m very stuck on this so any help appreciated.

    Code:
    List*		obstacle_list;
    List*		input;
    char 		line[1000];
    char		*token = OPC_NIL;	
    
    obstacle_list = op_prg_list_create();
    
    fgets(line, sizeof(line), some_file);
    
    while (line != OPC_NIL))
    {
    	token = strtok(line, "\t"); //Pull the string apart into tokens using the tabs
    	input = op_prg_list_create();
    	while (token != NULL)		
               {
               	   if (op_prg_list_size(input) == 0)
    		op_prg_list_insert(input,token,OPC_LISTPOS_HEAD);
    	   else
    		op_prg_list_insert(input,token,OPC_LISTPOS_TAIL);
    
                   token = strtok (NULL, "\t");
               }		
    	
                if (op_prg_list_size(obstacle_list) == 0)
    		op_prg_list_insert(obstacle_list,input,OPC_LISTPOS_HEAD);
    	else
    		op_prg_list_insert(obstacle_list,input,OPC_LISTPOS_TAIL);
    		
    	fgets(line, sizeof(line), some_file);				
    }
    
    size_ob_list = op_prg_list_size (obstacle_list);
    for (k = 0; k <size_ob_list; k++)
    {
    	line_coord_list = op_prg_list_create();
    	line_coord_list = op_prg_list_access (obstacle_list, k);
    					
    	count_inner_list = op_prg_list_size (line_coord_list);
    	for (j=0; j< count_inner_list; j++)
    	{
    		coords = (char*)op_prg_list_access (line_coord_list, j);
    		printf("%c", coords);		
    	}
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    how does your input loop terminate? the value of the variable 'line', which is an address on the stack, doesn't change and will never be 0, assuming your OPC_NIL == 0.

    fgets : On success, the function returns str. If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
    you should terminate the loop when fgets returns 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector element to char*
    By Ducky in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2012, 09:25 AM
  2. convert element of char* to a character
    By nicoeschpiko in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 11:39 PM
  3. Finding an element in a vector of pointers
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 01-30-2009, 06:25 AM
  4. Replies: 8
    Last Post: 04-23-2007, 09:22 AM
  5. Replies: 22
    Last Post: 10-22-2005, 08:42 AM