Function to return pointer to structure

This is a discussion on Function to return pointer to structure within the C Programming forums, part of the General Programming Boards category; Hi, How should I go about writin a function that takes no args and returns a pointer to a structure. ...

  1. #1
    Unregistered
    Guest

    Function to return pointer to structure

    Hi,
    How should I go about writin a function that takes no args and returns a pointer to a structure. the function prompts the user for some personal info and stores it in a structure.
    Now, I am new at using function with pointers, but I am assuming that the declaration would look like:
    Info* getdata ();
    the struct definition could look something like
    struct INFO{
    char name[];
    int age;
    };
    typedef struct INFO Info;
    the function would ask the user to enter name, then to enter age.
    how do you store the name and the age when you do the scanf?
    Now what should the function def look like?

    Thank you.

    Any additional info on functions that return pointers would be appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    struct somestruct * myfunction( void );

    There you go. This function, called myfunction returns a pointer of type struct somestruct.

    If you were using a typdef:

    typedef struct somestrut SOME;

    SOME * myfunction( void );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    Hi,

    Your code should look like this:
    Code:
    typedef struct {
      char name[20];
      int age;
    } Info;
    
    Info* GetData() {
      Info *info;
    
      info = (Info *) malloc( sizeof(Info) );
      scanf("%s", info.name);
      scanf("%d", &info.age);
    
      return info;
    }
    You have to free() it later on. The malloc() is becoz you have to be able to access it outside GetData(). If you do not do a malloc() and return the address of a local variable instead, the variable would go out of scope as soon as the control exits the GetData() function (and AFAIK the compiler would flag a warning).

    Hope this helps.

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >The malloc() is becoz you have to be able to access it outside GetData()...
    use of 'static' will help you in avoiding this.

    I Prefer:

    Code:
    typedef struct {
      char name[20];
      int age;
    } Info;
    
    Info* GetData() {
      static Info info;
    
      scanf("%s", info.name);
      scanf("%d", &info.age);
    
      return(&info);
    }

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    Originally posted by shaik786
    use of 'static' will help you in avoiding this.

    I Prefer:

    Code:
     static Info info;
    Right, but if the programmer wishes to have multiple copies of this struct then one has to have a malloc().

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >Right, but if the programmer wishes to have multiple copies of this struct then one has to have a malloc().
    malloc() before calling this function and copy the return value of this function to the malloc()'ed area.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by shaik786
    >Right, but if the programmer wishes to have multiple copies of this struct then one has to have a malloc().
    malloc() before calling this function and copy the return value of this function to the malloc()'ed area.
    Yes but why? What would be the point? If you're going to just copy it somewhere else anyway, why bother even returning a pointer? Return by value.

    There is no point that I see in this case of using a static variable and then copying the data out later. This only complicates the issue.

    For the simplest solution, just make a function that creates a new instance, and if you like, make it fill the variable[s] at the same time. Then return them.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >Yes but why? What would be the point? If you're going to just copy it somewhere else anyway, why bother even returning a pointer? Return by value.
    Certainly, I agree, but since the subject of this thread was "Function to return pointer to structure", I only showed one way of achieving that.

  9. #9
    Unregistered
    Guest
    Thank you for the intersting discussion.
    Didn't know I had to use malloc. Got to go read up on it.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21