3

I've the question about typedef statement.

Here is the code how i always write this statement:

typedef struct name
{

}name_t;

And here is the another example how i can write that:

typedef struct
{

}name;

Question is: what is purpose of that ways?

Hasbridge
  • 173
  • 1
  • 5
  • 14
  • In the second case there is defined an alias for unnamed structure. That is it is supposed that the structure tag name is not required in the following code. – Vlad from Moscow Apr 28 '20 at 09:12
  • 1
    Does this answer your question? [typedef struct vs struct definitions](https://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions) – RobertS supports Monica Cellio Apr 28 '20 at 09:19
  • When I don't use the tag name for a structure, I sometimes do `typedef struct /*untagged*/ { int foo; } whatever;` though I tend to *tag* all my structures. – pmg Apr 28 '20 at 10:14

3 Answers3

2

You need to use the first format if you have to refer to the type before the typedef is completed. This is necessary if the structure contains a pointer to the same type. This comes up when defining linked lists.

typedef struct name
{
    int value;
    struct name *next;
}name_t

You can't use name_t *next; inside the structure declaraction, because name_t isn't defined until later.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1
typedef struct name
{

}name_t;

name here is a struct tag and superfluous for the normal use-case, since you end up with a typedef anyhow and should only use name_t in your code from there on.

The only reason you'd add a tag there is the special case where you need a self-referencing struct such as a struct name* next; member in a linked list or such. In such cases we have to use the struct tag since the typedef name cannot be used before the typedef statement itself is done. Another alternative to that is to forward declare the struct.

The _t naming is industry standard way of naming types since the dawn of time. Unfortunately, the POSIX standard had the misguided idea to disallow such naming, so if you care about POSIX compliance, you should not name your types with _t in the end. If you don't care about POSIX, you should use _t because it gives proper, self-documenting code.

Lundin
  • 195,001
  • 40
  • 254
  • 396
1
typedef struct name
{
    int a;
    double b;
    char c;
}name_t;

name is a structure tag while name_t is a new type created by typedef.

To provide a structure tag explicitly is useful, if:

  1. You want to implement a forward-declaration of the structure for use it in f.e. function definitions or other structures before the structure definition occurs (important if you have several translation units/C files).

For example:

File1.c

struct name;   // forward declaration of structure `name`.

void foo(struct name *ptr)
{
    ....
}

struct bar
{
   int x;
   struct name y;
}

File2.c

typedef struct name
{
    int a;
    double b;
    char c;
}name_t;
  1. You want to define a pointer to an object of the structure itself inside of the structure, like required by linked lists:
typedef struct name
{
    int a;
    double b;
    char c;
    struct name *next;    
}name_t;