Thread: returning struct and passing as an argument

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    returning struct and passing as an argument

    Is there anything technically wrong in doing the following

    Code:
    typedef struct a
    {
         /* various data types */
    } data;
    
    data getInfo(data in)
    {
         /* do stuff with data types one or all */
    
         return in;
    }
    
    int main(void)
    {
        data test = getInfo(test);
        /* clean up */
    }
    If there is a problem, what would rectify the error(s)?

    TIA
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Did you try to write a simple program with it and compile it? Did it produce any results? You can find out that way.
    Mr. C: Author and Instructor

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by Mister C
    Did you try to write a simple program with it and compile it? Did it produce any results? You can find out that way.
    Thanks for the reply Mister C.

    Yes, I tried a few things with the function and got the expected results, but I don't know if it's the prefered way of doing things. For example, if I wanted to write the struct to a file.

    Code:
    int writeF(FILE* out, struct mydata in)
    {
        /* write the entire struct to a file */
        /* return status of write */
    }
    Is there a better way or would that method be ok?
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why not do the same thing with a pointer?
    Code:
    size_t writeF(FILE* out, struct mydata *in)
    {
       return fwrite(in, sizeof(*in), 1, out);
    }
    Then you don't have to make a copy of the whole structure when you pass it to the function.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by Dave_Sinkula
    Why not do the same thing with a pointer?
    Code:
    size_t writeF(FILE* out, struct mydata *in)
    {
       return fwrite(in, sizeof(*in), 1, out);
    }
    Then you don't have to make a copy of the whole structure when you pass it to the function.
    Thanks Dave... I'll give it a shot.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  2. Replies: 3
    Last Post: 04-15-2008, 02:16 AM
  3. Replies: 2
    Last Post: 04-11-2008, 02:42 AM
  4. Passing a struct
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 03-14-2005, 11:02 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM