Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with linked list

Status
Not open for further replies.

klose86

Programmer
Aug 1, 2006
13
CR

I made a linked list struct in C to contain two elements: a char* variable and a int variable. Wrote an INSERT function that created a Node with its char* data.
Now I'm trying to read an array and pass some of its information to a list already defined, but it just doesn't work out. If I try to insert any CHAR* to the list, it works, but it doesn't if the char* comes from the array.

void SaveToList(char* array[], int n){
char* temp;
if((strcmp(array[0], "declare")) == 0) {
temp = array[1];
list= Insert(list, temp);
printf("%s\n", temp);
}
}

I made sure the information on the array is coming right to the method, I need to save the info on the position 1 of the array, then insert it in the list. But the list just doesn't insert it right, it stores something else. I print out the data to see that is right, and in fact, it is, but not on the list.
ANY IDEA?

I appreciate your help.
THAN YOU VERY MUCH
 
If I try to insert any CHAR* to the list, it works, but it doesn't if the char* comes from the array ... But the list just doesn't insert it right, it stores something else.
Maybe you could elaborate on exactly what is happening when you insert strings from the array? Also, what is the "something else" that it stores?

We'll also need to see more code to see what the linked list is doing.

Just a shot in the dark, but when you insert the string into the linked list, are you just storing the char* pointer, or are you mallocing new memory and copying the string?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top