Thread: question about typedef

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65

    question about typedef

    Hi, im confused about the use of the keyword typedef, the question is, when do i have to use it?
    and whats the difference between declaring structure variables in this way:
    Code:
    struct book {
      ........
      ........
    };
    struct book b1, b2... ecc;
    // or
    struct book {
      ......
      .......
    } b1, b2....ecc;
    and this way:
    Code:
    struct book {
      ......
      ......
    };
    typedef struct book b1, b2..... ecc;
    Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Without the typedef you need to put struct before any declaration.

    Code:
    struct book b1; /* Without the typedef */
    book b2; /* With the typedef */
    The same goes for enums by the way.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The latter doesn't declare any variables, "b1" has become a type (identical to "struct book" -- that is, it is an alias). The former declares instances of "struct book"s. They're not the same thing in this circumstance.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    ok thanks everybody

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: redefinition of typedef xxxx
    By Rocha in forum C Programming
    Replies: 2
    Last Post: 11-24-2008, 09:19 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Help...typedef struct...confusing...
    By darkchild in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 08:03 AM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. question about typedef
    By volk in forum C++ Programming
    Replies: 8
    Last Post: 05-30-2003, 10:53 PM