Typedef Struct
Given the following example in Struct:
#include <stdio.h>
struct Student {
char *name;
int age;
};
int main(void) {
struct Student student = { "Austin", 9 };
struct Student *studentPointer = &student;
printf("%s is %d\n", studentPointer->name, studentPointer->age);
}
Now every single time we want to declare this struct Student
we have to use the keyword struct
.
Using typedef
can shorten it to just be calling Student
:
typedef struct Student {
char *name;
int age;
} Student;
Now you can just refer it as Student
instead of struct Student
:
#include <stdio.h>
typedef struct Student {
char *name;
int age;
} Student;
int main(void) {
Student student = { "Austin", 9 };
Student *studentPointer = &student;
printf("%s is %d\n", studentPointer->name, studentPointer->age);
}