Struct/typedef

This is a discussion on Struct/typedef within the C Programming forums, part of the General Programming Boards category; What's the difference between Code: typedef struct // about this line { int val; struct ll * next; } ll; ...

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    Struct/typedef

    What's the difference between

    Code:
    typedef struct // about this line
    {
    	int val;
    	struct ll * next;
    } ll;
    and

    Code:
    struct ll  // about this line
    {
    	int val;
    	struct ll * next;
    } ll;
    ? They both compile fine.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    The first one creates an anonymous struct (without any name) and typedefs it to the type "ll".
    The second one creates a struct called "ll" an initializes one struct called "ll".

    The first one is equal to:
    Code:
    struct someStruct {
    	int val;
    	struct ll * next;
    };
    
    typedef someStruct ll;
    the second one is equal to:
    Code:
    struct ll  // about this line
    {
    	int val;
    	struct ll * next;
    }
    
    struct ll ll;

Popular pages Recent additions subscribe to a feed

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