I need to create a structure inside a function (dynamically with malloc) Then i need to be able to send it to my main, and use it there. I have no problems creating it, I simply need help sending it to my main, and I'm also unsure of how to access it once I get it there.
struct retValue * fn() {
struct retValue
{
int number;
};
struct retValue* st = malloc(sizeof(*st));
return(???);
}
That is the code i have so far.
Thanks for any help. Let me know if you need something clarified.
EDIT:
Ok Some clarification is needed.
What I am trying to achieve, is the ability to pass a structure through a function to my main. Inside the function i must declare variables, and assign them values. Then in the main I must print each variable of the structure to the screen. No global variables can be used (and thus i assume no global structures).
Hopefully that clarifies things.
EDIT 2:
I've figured out my problem. For those interested, I needed to have the structure prototype outside of my functions first. Then i could pass st and then access it properly. Thanks to all, and sorry for the poor wording.
A structure can be returned from a function using the return keyword. Structures can be passed into functions either by reference or by value. An array of structures can also be passed to a function.
Return struct from a function Here, the getInformation() function is called using s = getInformation(); statement. The function returns a structure of type struct student . The returned structure is displayed from the main() function. Notice that, the return type of getInformation() is also struct student .
One of them asked if you could return a struct from a function, to which I replied: "No! You'd return pointers to dynamically malloc ed struct s instead."
A structure can be passed to any function from main function or from any sub function. Structure definition will be available within the function only. It won't be available to other functions unless it is passed to those functions by value or by address(reference).
// make the structure def global so that main() and fun() know about it.
struct retValue {
int number;
};
// function fn returns a pointer to the structure retValue
struct retValue * fn() {
struct retValue* st = malloc(sizeof(*st));
// return the pointer to the newly allocated structure.
return(st);
}
int main() {
// call fn() to create a new structure dynamically.
struct retValue *st = fn();
}
You will get hundreds of answers to this question. :)
You can return a pointer directly. E.g. return st
, Your main function will then be able to access the members of the struct as in st->number
. Make sure main free
s the pointer when it's done.
This is not necessarily a great design pattern. Your callee (fn) is allocating memory, but your caller (main) needs to deal with the aftermath. You can also structure this with main allocating the memory (using malloc or on the stack directly) and passing in the pointer for fn to use. Then the malloc/free happen in the same place. Otherwise you want to be sure to name your fn function something clear like allocateMyStructure
so it's clear that the return needs to be freed.
P.S. I'm not sure about rules for defining structures inside of a function, but you shouldn't do that here. Declare struct retValue above main and your fn function, so they can both "see" the declared type.
One thing I would like to point out...since the structure is local to the function, is there a global structure that matches exactly that, otherwise you could run into trouble, how would main()
know what is in that structure? Can you clarify?
Edit: It seems my answer has confirmed what I thought.
struct retValue { int number; }; int main(int argc, char **argv){ struct retValue *ptr; ptr = fn(); /* do what you have to do... */ .... return 0; } struct retValue * fn() { struct retValue* st = malloc(sizeof *st); /* do you work on the structure */ st->number = 5; return st; }
Incidentally somebody posted this earlier on today.
Hope this helps, Best regards, Tom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With