tags:

views:

71

answers:

2

Given the following code:

typedef struct IntElement
{
    struct IntElement *next; // use struct IntElement
    int         data;
} IntElement;

typedef struct IntElement
{
    IntElement *next; // Use IntElement
    int         data;
} IntElement; // why I can re-use IntElement here?

I use above data structure to define a linked-list node.

  1. Which is better one?
  2. Why I can use duplicated name (i.e. struct IntElement and IntElement in the typedef end)?
+8  A: 

Neither is C++. The first declaration is old C syntax. The second is someone who realized that you don't need that in C++ and then forgot it one line later. C++ does it like this:

struct IntElement {
    IntElement* next;
    int data;
};
DeadMG
+2  A: 

@Q1: Neither in C++. Just use

struct IntElement { 
    IntElement *next;
    int data;
};

struct IntElement is automatically typedef'd to IntElement

eq-