Search code examples
cstructtypedef

What is purpose for different ways of naming of typedef statement?


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?


Solution

  • 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.