Thread: typedef

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    typedef

    I sort of understand the typedef keyword like this.
    typedef int count;
    that will make count the same as int. so everytime the compiler sees count it replaces it with int right?
    If that is right I also have a question about structures typedefing
    Code:
    struct info{
                 int x;
                 char name[10];
    }
    struct info mike;
    now I know you can do this
    Code:
    typedef struct info{
               int x;
               char name[10];
    }
    info mike;
    If what i said about earlier typedefs is right how does it get rid of the struct part. Thanx in advance.

  2. #2
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Your code is quite weird, first, you forgot the ';' at the end of the struct block in both examples, besides, the second one isn't complete, it should be:
    Code:
    typedef struct info {
        int x;
        char name[10];
    } info;
    info mike;
    About the way the compiler does, it is a little more complex, a typedef defines an equivalent in all points to a given type, not just a replacement.

    example:
    Code:
    typedef int *intp;
    intp p1, p2;  /* Here, both lpv and pv are int * */
    int *pn, n;  /* Only pn is a int *, n is a simple int */

  3. #3
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    typedef struct info {
        int x;
        char name[10];
    }info;
    Hmmm ... I think this is invalid code. "info" two times ... that doesn't work.
    This should be correct:
    Code:
    typedef struct {
        int x;
        char name[10];
    } info;
    
    info mike;

  4. #4
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I was always successful using this kind of code, now, maybe isn't it standard but my compiler did never complain. Well, to me these are different labels, "struct info" and "info" itself. The rule is probably, try by yourself.
    By the way, I usually use different tags but it seemed (to me...) that the code shown above (in the first post) was designed so that the same name could be used without the "struct" thing.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Hmmm ... I think this is invalid code.

    It is valid. The structure tag and the typedef are in separate identifier namespaces. Here is a better explanation.
    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.*

  6. #6
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by Dave_Sinkula
    It is valid. The structure tag and the typedef are in separate identifier namespaces. Here is a better explanation.
    You're right. Now it's quite obvious to me why the code is valid indeed. And thank you for the link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM