aelor
aelor

Reputation: 11116

Difference between these c struct declarations?

I have a code looking like this :

struct point {
   int a;
   int b;
}

which can be further used like this :

struct point p;
p.x = 10;
p.y = 5;

now I came to know that this can also be written like this :

typedef struct{
   int x;
   int y;
} point;

and can be used as point p

The confusion started when I started learning linked-list, this is the code I saw.

typedef struct node {
    int val;
    struct node * next;
} node_t;

I have a couple of questions:

  1. If we can define the struct by simply using typedef struct { ... } node whats the use of writing typedef struct node {.....
  2. The node_t at the end of the code is really confusing because from my understanding it has already defined a type node so we can call node x to create a node , then what's the need of the node_t or basically writing anything after the } what it means ?

shouldn't this work ?

typedef struct {
        int val;
        struct node * next;
    } node;

Upvotes: 3

Views: 256

Answers (4)

haccks
haccks

Reputation: 106002

  1. If we can define the struct by simply using typedef struct { ... } node whats the use of writing typedef struct node {.....

  2. The node_t at the end of the code is really confusing because from my understanding it has already defined a type node so we can call node x to create a node , then what's the need of the node_t or basically writing anything after the } what it means ?

In C, structures can have both tag and a typedef name. In the structure declaration:

typedef struct node {
    int val;
    struct node * next;
} node_t;   

node is a tag and node_t is typedef name. Latter you can declare your structure variable either by using tag or typedef name.

struct node *new_node;   // OK  
node_t *head;            // OK    

In fact, the tag and typedef name can even be same, although that's not required:

typedef struct node {
    int val;
    struct node * next;
} node;  

shouldn't this work ?

typedef struct {
    int val;
    struct node * next;
} node;  

No. This will not work . Why?
The reason is that, when a structure has a member that points to the same kind of structure, as node does, we are required to use a structure tag. Without the node tag, we would have no way to declare the type of next.


Suggested reading: As Op is asking for good resource on data structure. Here you can go:

  1. Tutorial: Introduction to Data Structures.
  2. Book: Classic Data Structures.

Upvotes: 7

Doonyx
Doonyx

Reputation: 590

Here is my explanation.

struct
{
   int i;
};

declares an unnamed structure and apparently there is no real usage of this. Please correct me if I am wrong.

struct
{
   int i;
} T;

now T is an object in the given scope and type is unnamed.

struct TT
{
   int i;
} T;

declares a structure TT and an object T. You can use TT to create more objects. The best way to think about this is, just consider struct TT{ int i} as a single type such as double.

typedef struct TT
{
   int i;
};

has the same meaning as (at least in nowadays compiler. I'm not sure about the older ones)

struct TT
{
   int i;
};

and

typedef struct
{
   int i;
} TT;

declares a type TT (not an object) for the given structure as you are using typedef int Int;

Hope this resolves the mess.

Upvotes: 0

Peach
Peach

Reputation: 97

Here, you can also use 'struct p' by typedefing it using 'struct p typedef P;'. It sometimes makes me confused when typedef a complex function pointer. I don't know the reason why the C std. support such a syntax sugar.

Upvotes: 0

Happington
Happington

Reputation: 454

The third block of code, which you seem to have a question about, has a redundant identifier.

typedef struct node {
    int val;
    struct node * next;
} node_t;

is identical to

struct node{
    int val;
    struct node * next;
};
typedef struct node node_t;

They're identical, but merged into the same line. The syntax around a typedef is

typedef [original name] [new name];

Normally, when we typedef a struct, the ONLY way to reference it afterwards would be the typedeffed name (in the first code example, you could only access it by node_t) while a slightly more redundant declaration allows for accessing by the "struct node" variable type too.

To answer your other question the node_t that confused you is what you can refer to the struct as... for example

node_t node1;
struct node node2;

Are the two valid declarations for a node struct.

Upvotes: 4

Related Questions