Leon Trotsky
Leon Trotsky

Reputation: 350

Typedef and struct in C

Is there any difference between those two:

typedef struct ddwq{
    int b;
}ta;

typedef struct {
    int b;
}ta;

Upvotes: 3

Views: 87

Answers (1)

dbush
dbush

Reputation: 223689

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.

Upvotes: 7

Related Questions