开发者

Return Structure from Function (C)

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.


// 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. :)

  1. 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 frees the pointer when it's done.

  2. 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.


just: return st;


Please declare the structure in a common header file and include it in your source file and then return st from your current function. That should work.


struct retValue { int number; };

int main(...) {
  struct retValue *myval;

  myval = fn();
}

struct retValue *fn() {
  struct retValue* st = malloc(sizeof(*st));

  return st;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜