[Solved] pass pointer of struct in c programming


Firstly, this question looks fairly ridiculous, especially with a struct name of “Nooooooooooooooooooooooo”, it’s probably a waste of people’s time trying to answer.

Secondly, your terminology is way off. Passing a pointer to a structure is very different from a pointer to a function!

However in your code, the main issue is with this line:

void GetData(course *newnode,course *root,course *last)

Do you actually know what you have here? Well, you have 3 local pointers which when your program starts are all null or uninitialised. Then in your function, you malloc() some ram and use these local pointers to store the address of this allocated block of memory. However, you don’t seem to understand that these are local copies of the pointers that you’ve passed in, so when your function ends, they’re going to disappear when the stack unwinds.

If you’re going to want to return the address of the memory you allocate to the calling function, you’re going to need to pass the address of the pointer and then make your function take a double dereference.

Something like this…

course *c;

GetData(&c);

void GetData(course **c)
{
    *c = (course*)malloc(sizeof(course));
    ...
    ...
}

2

solved pass pointer of struct in c programming