Thread: Difference between typdefing and declaring structs

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    Difference between typdefing and declaring structs

    What's the difference between this:
    Code:
    struct aStruct {
           int aNumber;
    };
    and
    Code:
    typedef struct {
          int aNumber;
    } aStruct;
    All I can think of is maybe the compiler will replace aStruct with the actual struct{} definition with typedef?
    Using Dev-C++ on Windows

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    this might explain what you are wondering

    http://msdn.microsoft.com/library/de...m/tions_14.asp

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    I think that the typedef is common in C so you dont have to type struct each time you declared an object (or use one) of that structure. But essentially they are the same thing.

    so you dont have to do:
    Code:
    struct Foo {
       int a, b;
    };
    
    int main () {
       struct Foo testData;
    }
    Instead you can do...
    Code:
    typedef struct {
       int a, b;
    } Foo;
    
    int main () {
       Foo testData;
    }
    It's usefull in C because you must specify a struct with "struct" even when declaring or passing the struct, so the client doesn't need to know that Foo is a structure.
    Last edited by JoshR; 07-20-2005 at 11:06 PM.

  4. #4
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    So really it has no difference in C++, just C?
    Using Dev-C++ on Windows

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by Ganoosh
    So really it has no difference in C++, just C?
    Correct. They are the same thing, its just in C you have to use the word "struct" before all variables of a structure. A typedef is a way to get around that.

  6. #6
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    I see
    Using Dev-C++ on Windows

Popular pages Recent additions subscribe to a feed