I’ve got a function which is supposed to add a file to a node of a linked list, each file is a structure containing a constant char *name. The function uses parameters of a linked list and a const char[] (which will name of new data struct). Everything goes smoothly until I try to call the function again, the name of my first data gets changed to the new one, I think I am forgetting to allocate memory somewhere. (or I am screwing up with the const chars)
(here’s the section of code which puts const char[] new_name into a spot in the list)
node->data_list[node->data_count].data_name = new_name;
node->data_count++;
if
data_nameis achar*andnew_nameis achar[]this:does not copy
new_nameintodata_namebut just means thatdata_namepoints tonew_name. Eachdata_namein the list will be pointing tonew_name.You need to
malloc()andstrcpy():Remember to:
when no longer required.