Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 274
aelor Avatar asked Sep 24 '26 21:09

aelor


2 Answers

  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.
like image 181
haccks Avatar answered Sep 27 '26 16:09

haccks


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.

like image 34
Happington Avatar answered Sep 27 '26 18:09

Happington