> I'm getting file statistics (just name and size for now) and sticking them
> in a linked list. The linked list is ordered in descending size and based on
> a program from a text book explaining linked lists and how to insert new
> records into them.
> The file path prints out garbage but the sizes look OK and in order. I think
> I've screwed up a string or a pointer. The individual records seem to get
> created OK as I've put test points all over the place and got the expect
> results. Can anyone see what's wrong with the list.
to allocate space for the pathnames of each file. You have your pointer point
to the local array declared in your function, which will be invalid once you
leave the function. Thus a working version would look like:
else {
ptr->path = strcpy (calloc (sizeof (char), strlen (path_name) + 1),
path_name);
ptr->size = statbuf.st_size;
ptr->next = NULL;
This explicitely allocates enough dynamic memory to fill in the string (plusQuote:}
the terminating \0) and then copies the string into it.
Same applies if you wanted to free the list. You'd have to free those pathname
strings first and then the list item itself.
Cheers!