Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typedef and struct in C

Tags:

c

struct

typedef

Is there any difference between those two:

typedef struct ddwq{
    int b;
}ta;

typedef struct {
    int b;
}ta;
like image 564
Leon Trotsky Avatar asked Dec 12 '25 07:12

Leon Trotsky


1 Answers

In the former case, you can reference the type of the struct as either struct ddwq or ta. In the latter case, you can only reference it as ta since the struct has no tag.

The first case is required if the struct will contain a pointer to itself such as:

typedef struct ddwq{
    int b;
    struct ddwq *p;
}ta;

The type name ta isn't visible inside of the struct, so the struct must have a tag name for it to reference itself.

like image 151
dbush Avatar answered Dec 13 '25 19:12

dbush