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);
}
}