The trouble is that my programme is printing out a load of rubbish when I try and print out the Linked List.

Its printing out a load of characters such as dollar signs and maths symbols. I figure this is because I'm printing out the element out in the wrong format. Can anyone give me a hand figureing out where I am going wrong please?

in that last loop there. Because otherwise if z is null it will try to access z->next and then you will segfault.

05-08-2006

Wiretron

Thanks for the reply.

Tried that but it's still printing out the same rubbish.

05-08-2006

Quantum1024

Code:

while(z->next != NULL) {
printf("%c", z);

Here z is the pointer to a list node and so can't be printed as a charactor. If you want to print the charactor in the node then use z->X[0]

Also there's no point in creating arrays with a size of one you might as well just create a single char if thats what you want or if you want a full word in each node then create a char * and assign it's memory dynamically.

You can't strcpy into a location that can only hold a single character - you can but you'd only be able to successfully copy 0 length source strings (NULL terminator only) which would be rather pointless.

Code:

List *z = NULL;

...

printf("%c", z);

z is a pointer to a List node, IT IS NOT a character. Do you mean instead:

Code:

printf("%c",z->x[0]);

Code:

while(z->next != NULL)
{
...

z = z->next;
}

By reassigning the z pointer, you lose the start of the list and therefore any hopes of freeing the memory you allocated. This is sloppy. You should keep two pointers, one stays pointed to the head of the list, the other is used to walk through the list and help with the printing.