Diff btw typedef struct and struct?

This is a discussion on Diff btw typedef struct and struct? within the C Programming forums, part of the General Programming Boards category; Whats the difference btw just struct and typdef struct?...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    70

    Diff btw typedef struct and struct?

    Whats the difference btw just struct and typdef struct?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

  3. #3
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    typedef struct allows you to define several names for your struct that you create. Here's an example of a normal struct:

    Code:
    //Regular struct declaration:
    struct Sprite{
       int HP, MP, Defense, Attack;
       Sprite(); //Yada, yada.
    };
    
    //Typedef struct declaration:
    typedef struct Sprite{
       int HP, MP, Defense, Attack;
       Sprite(); //Yada, yada.
    } sprite, SPRITE, _SPRITE_;

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    That's true, but most of the time typedef is used because no one wants to type "struct" everytime they declare an instance of the struct.

    Code:
    struct Sprite{
       /* ... */
       Sprite(); //Yada, yada.
    };
    Um . . . this is C.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. typedef struct
    By ... in forum C++ Programming
    Replies: 5
    Last Post: 01-19-2004, 03:17 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21