differences between struct and typedef struct

This is a discussion on differences between struct and typedef struct within the C Programming forums, part of the General Programming Boards category; This may have been asked before and may also be trivial, but I'm curious between the differences of: Code: struct ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    differences between struct and typedef struct

    This may have been asked before and may also be trivial, but I'm curious between the differences of:
    Code:
    struct ANYTYPE
    {
        // members
    };
    and:
    Code:
    typedef struct TYPENAME
    {
          // members
    } ANYTYPE;
    Would be muchly appreciated to hear some thoughts.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    When you name a structure with a tag like so,
    Code:
    struct tag {
    
    };
    you're declaring a structure tag that can be used together with the struct keyword to declare an instance of that structure.
    Code:
    struct tag instance;
    When you use a typedef like this,
    Code:
    typedef struct {
    
    } tag;
    you're declaring a typedef by the name of tag which happens to map to a structure, and which you can use like this, without the "struct" keyword.
    Code:
    tag instance;
    Basically, using typedef lets you leave off the struct keyword everywhere else.

    (BTW, this is a C phenomenon. In C++, you can declare an instance of a struct or a class with just the tag name, with no typedef required.)
    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. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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