Thread: Struct, Pointers a function creation

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    56

    Struct, Pointers a function creation

    So im studying structs and i have a question about them

    if you have something like this:

    Code:
    struct Person {
      int age;
      int SSN;
      int weight;
    };
    Then i want to make a function

    Code:
    struct Person *Person_make(int age, int SSN, int weight)
    {
    blah blah
    }
    Why do i have to use the pointer notation before the function name??

    ie *Person_mane ???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SuperMiguel
    Why do i have to use the pointer notation before the function name??

    ie *Person_mane ???
    Actually, the syntax is:
    Code:
    RETURN_TYPENAME Person_make(int age, int SSN, int weight)
    {
        /* ... */
    }
    Where RETURN_TYPENAME is: struct Person *
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    So that function must return a pointer of type Person?

    like Person *c = malloc(sizeof(*c));

    return c;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes (rather, struct Person, not Person, unless you have the appropriate typedef).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by laserlight View Post
    Yes (rather, struct Person, not Person, unless you have the appropriate typedef).
    Yup, cool thanks

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Well i have one more question...

    If i use typedef.. for example:

    Code:
     
    
    Typedef struct { int age; int SSN; int weight; }Person;


    i can do this: Person *test = malloc(sizeof(*test) ?? instead of with out typedef using struct Person *test = malloc(sizeof(struct Person)

    Like on the 1st example i can pass the name of the pointer i just created instead of the struct name?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can do sizeof(*test) no matter what the type of *test is, and it will always be correct.

    The only reason to typedef a type is to create another name for that type, for aesthetic purposes.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    thxs

  9. #9
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    If you typedef in that instance, it saves you having to keep typing struct. That's about the only advantage, but that can be a big one if you have a lot of these types floating about. I typedef structs by habit and only really type struct in the header files that define the typedef.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by SuperMiguel View Post
    Then i want to make a function

    Code:
    struct Person *Person_make(int age, int SSN, int weight)
    {
    blah blah
    }
    Why do i have to use the pointer notation before the function name??
    Actually, you don't have to.

    It is possible to return a struct by value (rather than returning a pointer). That does rely on the compiler having visibility of the struct definition (as opposed to just a forward declaration) though.

    Which is right? That depends on what you are trying to achieve. The two methods of returning a struct enable different things.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 07-06-2011, 11:44 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Typdef struct and function pointers
    By tempster09 in forum C Programming
    Replies: 1
    Last Post: 12-06-2009, 10:16 AM
  4. Object Creation in thread function
    By sl4nted in forum Windows Programming
    Replies: 18
    Last Post: 10-11-2008, 02:37 PM
  5. mode function creation
    By rogster001 in forum C Programming
    Replies: 2
    Last Post: 08-21-2006, 05:49 AM