Thread: different ways of typedefing

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    different ways of typedefing

    In this project I'm working on, they do this:

    Code:
    struct aggregator_t
    {
    	.... bunch of ints
    };
    
    typedef struct aggregator_t aggregator;
    
    aggregator *ags;
    Is this different than doing

    Code:
    typedef struct aggregator_t
    {
     .......
    } aggregator, *ags;
    If it's different, is there any prefered way?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The result is the same.

    If you are going to get into windows programming, they have settled on
    Code:
    typedef struct tagSTRUCTNAME
      {
         ... bunch of vars
       } STRUCTNAME, *PSTRUCTNAME;
    It pretty much all amounts to the same thing and your own style will likely evolve according to where you do most of your work... I do mostly windows api coding, so I prefer the windows way. Your mileage may vary

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    China
    Posts
    12
    They are different.
    In the 1st code, a structure variable named args is declared and then args is an object. In the 2nd code, however, args is a new alias for the type "struct aggregator_t *" .

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by orientuser View Post
    They are different.
    In the 1st code, a structure variable named args is declared and then args is an object. In the 2nd code, however, args is a new alias for the type "struct aggregator_t *" .
    I think the other guy is correct. I think args is a pointer in both cases

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dayalsoap
    I think the other guy is correct. I think args is a pointer in both cases
    Well, this is a case where one can construct a program to help determine the answer. For example:
    Code:
    typedef struct aggregator_t
    {
        int n;
    } aggregator, *ags;
    
    int main()
    {
        struct aggregator_t x;
        ags p = &x;
        return 0;
    }
    Now, if ags is an alias for struct aggregator_t* as orientuser stated, the above program should compile. On the other hand, if CommonTater is right, it should not compile, because one cannot use a variable name as a type name. So, what do you think is the answer now?
    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

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    @laserlight:

    I was not sure what would happen; your code example was enlightening.

    @CommonTater: You might want to re-think your answer that they are the same.

    Tim S.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stahta01 View Post
    @laserlight:

    I was not sure what would happen; your code example was enlightening.

    @CommonTater: You might want to re-think your answer that they are the same.

    Tim S.
    I did not say they were the same...

    "It pretty much all amounts to the same thing"

    That is to say you end up with a struct and a pointer to the struct....

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    I did not say they were the same...

    "It pretty much all amounts to the same thing"

    That is to say you end up with a struct and a pointer to the struct....
    Very well, it looks like I have to take a direct approach: you're wrong. In this code snippet, aggregator is an alias for struct aggregator_t, and ags is a pointer to a struct of that type:
    Code:
    struct aggregator_t
    {
        int n;
        int m;
    };
    
    typedef struct aggregator_t aggregator;
    
    aggregator *ags;
    But in this code snippet, aggregator is an alias for struct aggregator_t, and ags is an alias for struct aggregator_t*:
    Code:
    typedef struct aggregator_t
    {
        int n;
        int m;
    } aggregator, *ags;
    Since ags in the former is not the same as ags in the latter, the code snippets do not amount to the same thing.
    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

  9. #9
    Registered User
    Join Date
    Sep 2010
    Location
    China
    Posts
    12
    Quote Originally Posted by orientuser View Post
    They are different.
    In the 1st code, a structure variable named args is declared and then args is an object. In the 2nd code, however, args is a new alias for the type "struct aggregator_t *" .
    sorry, i made a mistake. In the 1st code snippet, args is a pointer to struct aggregator_t, not a structure variable.
    Last edited by orientuser; 09-15-2010 at 04:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. any smarter ways to design this tree structure
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 04-19-2008, 07:34 AM
  2. Smarter ways to define MACRO to strings?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-22-2007, 02:05 AM
  3. Best ways to learn
    By MartijnG in forum C++ Programming
    Replies: 15
    Last Post: 08-29-2006, 10:51 AM
  4. Ways to Write IF Statements...
    By mbolthouse in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2001, 06:37 PM