typedef struct queue_item_t { // 1
vpoint_t void_item;
struct queue_item_t* next; // 2
} queue_item_t; // 3
First of all, note that this entire statement is defining a typedef
.
3) Is saying that the new type we are in the process of defining (via typedef
) is going to be named queue_item_t
.
1) The name of the structure (which is being given a new name, as we go), is named struct queue_item_t
. That's it's full name, including struct
at the front.
2) Because the new type doesn't yet exist (remember, we're still in the process of defining it), we have to use the only name it has thus far, which is struct queue_item_t
, from 1).
Note that you can have anonymous struct
definitions, which allow you to omit the name from 1). A simple example:
typedef struct {
int x, y, z;
} vector3;
In your example however, since we need the structure to be able to refer to itself, the next
pointer must have a type that's already defined. We can do that by forward declaring the struct, typedefing it, then defining the struct using the typedef
d type for next
:
struct _queue_item; // 4
typedef struct _queue_item queue_item_t; // 5
struct _queue_item { // 6
vpoint_t void_item;
queue_item_t* next; // 7
}
4) Declare that struct _queue_item
exists, but don't yet provide a definition for it.
5) Typedef queue_item_t
to be the same as struct _queue_item
.
6) Give the definition of the structure now...
7) ...using our typedef
'd queue_item_t
.
All that being said... In my opinion, please don't use typedefs for structs.
struct queue_item {
void *data;
struct queue_item *next;
}
is simple and complete. You can manage to type those six extra characters.
From the Linux Kernel coding style:
Chapter 5: Typedefs
Please don't use things like "vps_t".
It's a mistake to use typedef for structures and pointers. When you see a
vps_t a;
in the source, what does it mean?
In contrast, if it says
struct virtual_container *a;
you can actually tell what "a" is.
There are exceptions, which you can read about.
Some recent related questions: