Return Structure from Function (C)

14,886

Solution 1

// 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();
}

Solution 2

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.

Solution 3

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.

Share:
14,886
Blackbinary
Author by

Blackbinary

CIS Major 1st Year

Updated on June 21, 2022

Comments

  • Blackbinary
    Blackbinary almost 2 years

    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.

  • David Thornley
    David Thornley over 14 years
    I don't think it's a problem allocating memory in a function, as long as the function name is clear about it (something like CreateNewFoo() or something). Otherwise, I agree with you.