"ptr" goes away, yes. It isn't a dangling pointer, it's a NONEXISTENT pointer. However, you RETURNED it's value to the calling code, so that code now has a copy of it. That presumes you called it something like this:

pMyNode = create (head, val);

pMyNode now holds the value that was previously held by 'ptr', which is now gone bye-bye. You must save these values, so that you can free them later. Normally, that would be done in the 'next' pointers, so one would begin at the head and free all memory associated with the 'next' pointers until a NULL pointer was found.

Pay attention. "ptr" was destroyed. Before it was destroyed, its contents were transerred out of the function, via the return mechanism, to another variable that now contains the value returned by malloc, which points to the dynamic memory that was allocated. If you transfer a candy bar from your right hand to your left hand, then I chop off your right hand, you still have the candy bar.

Isn't that suppose to be a characteristic example of a dangling pointer.
Before "line" is destroyed, aren't its contents tranfserred out of the function, via the return mechanism ?
Sorry again if I insist but I must understand it well

You're code is incomplete. It means nothing. There is no such thing as a 'dangling' pointer, despite all the protestations of various gurus that you might feel compelled to kneel before, and kiss the hems of their garments. There are valid pointers and invalid pointers. It baffles me that you cannot understand that valid information can be preserved, despite the death of its originator.

You're code is incomplete. It means nothing. There is no such thing as a 'dangling' pointer, despite all the protestations of various gurus that you might feel compelled to kneel before, and kiss the hems of their garments. There are valid pointers and invalid pointers. It baffles me that you cannot understand that valid information can be preserved, despite the death of its originator.

Click to expand...

First thing first : If it baffles you to answer something then simply don't answer it !
And about my code, is not incomplete, is just a Code Snippet focused on what I'm interesting for....
Is just a simple question ::::::

First of all, if you can't hack a little criticism, you're getting into the wrong business. If you refuse to think, you're definitely getting into the wrong business.

I call what you posted incorrect code. It also doesn't match your snippit, which makes your snippet no good for explaining the problem. The code that you show is full of woes. You cannot return the address of the array and do any good because THE ARRAY goes away. You have the address of where it was, which, because it is an autovariable, is useless information. When you use malloc, the array exists on the heap. The pointer to it is an autovariable, so you will lose THAT, but the array remains. If you return the value of the pointer to main, main now has the heap address stored in ITS autovariable. It doesn't matter that you lost the one in 'createstring'.

Further, do not use 'gets'. It puts no constraint on how many characters you are willing to accept. If the user enters 1001, which is merely half of a full screen, your program is toast. Use 'fgets'.

Your statement, 'line points to 0xNothing' is not correct. line points to precisely the memory that it pointed to before, but that memory NO LONGER CONTAINS LINE, or, at least, won't in the very near future. Get that data area from the heap, and it'll be there until you free it.

I recommended that you study autovariables and scope. Clearly, you still need to do that. You also need to read this.

There is a library function that performs your createstring function. It is called 'strdup'.

Yes, that's a good answer. You got the same answer here. It seems likely that you are going to pay no attention to either of them. You might want to get your hand fitted for a mop, because janitorial duties might well be your future.

If you ever dropped a competing forum's link on MY forum, I would warn you once, then ban you.

A dangling pointer does not exist, pointers are valid or not. If a pointer is invalid, it means it does not point to a valid block of memory. Which in turn means you forgot to allocate memory for it or did not point it to a particular block of memory.