Problems with recursive structure

This is a discussion on Problems with recursive structure within the C Programming forums, part of the General Programming Boards category; Hello,
I'm trying to understand the logic of recursive structures (or is "linked list" much proper term?) and I have ...

So far so good, but you don't need to cast the return value of malloc, and it would be a little better to use sizeof(*p), e.g.,

Code:

PERSON *p = malloc(sizeof(*p));

Next, we see:

Code:

p = *p_obj;

This must be a mistake. You just made p point to some dynamically allocated memory, and now you assign to p. At the very least, this leads to a memory leak.

If you want to keep the parameters of add_new_person as-is, then you should be checking if *p_obj is a null pointer. If it is, then you assign p to *p_obj. Otherwise, you search for the last element in the linked list, and assign p to its next pointer. You should only call malloc once in add_new_person. In main, nice_person should be initialised as a null pointer.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.