Thread: Struct/typedef

  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