If I created a structure:
struct student {
int id;
char name [20];
student * next};
What is the meaning of:
typedef struct student student?
What does student * next
?
Thank you!
If I created a structure:
struct student {
int id;
char name [20];
student * next};
What is the meaning of:
typedef struct student student?
What does student * next
?
Thank you!
A typedef
creates a new name. Thus these are completely equivalent:
student *next;
struct student *next;
In your example the struct and the typedef have the same name. But it could be like this:
struct something {
/* ... */
};
typedef struct something student;
The declaration of a typedef looks like the type it introduces.
As a side side note, things are different in C++.
typedef struct student student;
This create an alias for struct student
with the name student
.
Structure tags and type names live in two different namespaces in C (not true in C++), so it is allowed to use the same name for the alias and for the tag.
In C, whenever using structs you have to use the keyword struct
. By typedefing typedef struct student student
, you're able to say struct student
by simply saying student
.
With a named structure, the keyword struct
most usually be used to create instances of the structure and to reference the structure type at all.
The typedef
allows you to virtually create an alias for student
instead of having to use struct student
constantly, thereby actually making it behave more like a built-in type.