Why typedef struct

This is a discussion on Why typedef struct within the C Programming forums, part of the General Programming Boards category; Hi, Why would one write something like Code: typedef struct tagSTRUCT { int n; } StructName; ... instead of Code: ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Question Why typedef struct

    Hi,

    Why would one write something like
    Code:
    typedef struct tagSTRUCT
    {
      int n;
    } StructName;
    ... instead of
    Code:
    struct StructName
    {
      int n;
    };
    What's the deal?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because the first can be used as
    Code:
    StructName foo;
    while the second must be used as
    Code:
    struct StructName foo;

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Other examples while combining both methods:

    Code:
    typedef struct
    {
            int a;
    } a;
    
    typedef struct b
    {
            int b;
    } b;
    
    int main(void)
    {
            a struct_a;
    
            b struct_b;
            struct b struct_b2;
    
            struct_a.a  = 0;
            struct_b.b  = 0;
            struct_b2.b = 0;
    
            return 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Arrow

    Hi,

    Thanks for your almost immediate replies. But this compiles perfectly:
    Code:
    #include <iostream>
    
    struct s
    {
        int a;
    };
    
    int main ()
    {
        s _s;
        std::cout << _s.a << std::endl;
        return 0;
    }
    I don't need to type:
    struct s _s;

    Why is that? I'm using Dev-C++ (the latest version) with the MingW compiler.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    That's because you're using C++, not C, and you don't need to typedef structs in C++.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,439
    Quote Originally Posted by MacGyver View Post
    That's because you're using C++, not C, and you don't need to typedef structs in C++.
    Using a C++ compiler actually. C++ supports some things that are not supported in C and a lot of ostensible C compilers in use are actually C++ compilers.

    You might also try looking for compiler options (eg command line settings) to make your compiler behave as a C compiler rather than as a C++ compiler. To do that, it is necessary to read the documentation for your compiler.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I thought iostream, cout and endl were C++.

    In C you need the typedef for convenience. In C++ you don't need it all (but the compiler accepts this syntax too).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Yup, they are. This is C++, not C however much you turn and spin it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  4. typedef struct
    By ... in forum C++ Programming
    Replies: 5
    Last Post: 01-19-2004, 02:17 PM
  5. typedef struct question
    By flevine100 in forum C Programming
    Replies: 1
    Last Post: 09-03-2002, 09:34 PM

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