This post was first published on Jupius Learning, where you can read it free with diagrams and practice exercises.
typedef gives an existing type a second name. When you write typedef struct student student_t, you can make a variable with student_t s1 instead of struct student s1. Nothing about the data changes. The typedef does not create a new kind of object, and it does not set aside any memory. It is only another name for a type you already have.
In the last post we declared a struct student, filled it and read it with a dot. You probably noticed that writing struct student every time is a bit long. This post is about that shorter name, and about why the Linux kernel uses typedef for some types and avoids it for most.
What does typedef do to a C struct?
Start with the struct from last time, then give it a typedef name.
struct student {
int roll_no;
char name[50];
float marks;
};
typedef struct student student_t;
You can also write both in one go:
typedef struct student {
int roll_no;
char name[50];
float marks;
} student_t;
After either of those, these two lines make the same kind of variable:
struct student s1 = { 1, "Asha", 87.5f };
student_t s2 = { 2, "Ravi", 91.0f };
struct student is still a valid type name. The word student after struct is the tag. student_t is the typedef name. They describe the same layout. s1.marks and s2.marks work the same way, with the same dot we used last time.
The _t at the end is only a naming habit. It is common in C, but it is not required. I will stick to student_t here because you will see that pattern a lot.
typedef with an anonymous struct
You do not have to give the struct a tag at all. If you leave the tag out, the typedef name is the only name the type has.
typedef struct {
int roll_no;
char name[50];
float marks;
} student_t;
Now student_t s1; works, and struct student s1; does not, because there is no tag called student. The struct is anonymous. This is shorter to type. The cost is that the type has only one name, which matters as soon as you need to mention the type before it is fully defined.
The Linux kernel uses this shape for sigset_t, a signal mask. In a modern kernel it looks like this, in simplified form:
/* Simplified from the Linux kernel */
typedef struct {
unsigned long sig[_NSIG_WORDS];
} sigset_t;
_NSIG_WORDS is a kernel macro for how many unsigned long values it takes to hold every signal bit. On an older 32-bit kernel you may see sig[2], which is the same idea with the number filled in. You treat sigset_t as its own type, and you usually change it through helper functions rather than writing to the sig field yourself.
The forward-declaration case
Sometimes you need to mention a struct type before you have written its fields. A function prototype is a common reason. A field that points to the same type of struct is another. In C you can give the compiler the typedef name early, as long as the struct has a tag.
typedef struct student student_t; /* the typedef comes first */
void print_student(const student_t *s);
struct student {
int roll_no;
char name[50];
float marks;
student_t *partner; /* a pointer is fine here */
};
The typedef line does not describe the fields yet. It only says there is a struct student, and student_t is another name for it. A pointer to an incomplete type is allowed, so the prototype and the partner field both compile. You cannot make a student_t variable until the braces that list the fields have been seen.
If you try to put the typedef name inside the same typedef that creates it, that does not work:
typedef struct student {
int roll_no;
/* student_t *partner; */ /* too early: student_t does not exist yet */
struct student *partner; /* this is fine: the tag already exists */
} student_t;
The tag student is visible from the opening brace. The typedef name student_t exists only after the semicolon that finishes the typedef.
An anonymous typedef cannot be written this way. There is no tag to mention in a forward line, so you cannot say the type exists and fill in the fields later. If you think you will need a forward declaration, keep the tag.
The -> in a function like print_student is the arrow I mentioned last time. You use it when you have a pointer to a struct. We will spend a later post on pointers. For the rest of this post, the examples in main still use a plain variable and a dot.
A typedef that hides a pointer
typedef can name a pointer type too:
typedef struct student *student_ptr;
student_ptr p;
p is a pointer, but the name does not show that. student_ptr p; looks a lot like student_t s;, which is a struct variable, not a pointer. If you later write p.marks, the compiler will complain, because a pointer needs p->marks. That mix-up is easy to miss when you read someone else's code.
This is the main reason the Linux kernel coding style says not to typedef ordinary structs or pointers. When you see a line like vps_t a;, you cannot tell if a is a struct or a pointer. It could even be an integer. When you see struct virtual_container *a;, you can. I will come back to that rule after we look at the typedefs the kernel does keep.
A complete C example you can run
Here is the named typedef, used the same way we used struct student in the last post.
#include <stdio.h>
#include <string.h>
typedef struct student {
int roll_no;
char name[50];
float marks;
} student_t;
int main(void)
{
struct student s1 = { 1, "Asha", 87.5f };
student_t s2 = { 0 };
s2.roll_no = 2;
strcpy(s2.name, "Ravi");
s2.marks = 91.0f;
printf("%d %s %.1f\n", s1.roll_no, s1.name, s1.marks);
printf("%d %s %.1f\n", s2.roll_no, s2.name, s2.marks);
return 0;
}
Save it as student_t.c, then compile and run it:
gcc student_t.c -o student_t
./student_t
On Windows with MinGW installed, the second line in PowerShell is .\student_t.exe.
Output:
1 Asha 87.5
2 Ravi 91.0
s1 uses the tag name. s2 uses the typedef name. Both are the same type, so the rest of the program does not care which spelling you picked.
typedef in the Linux kernel: wait_queue_head_t
The Linux kernel does use typedef on some structs, including a few older names that are still around. One you will meet often is wait_queue_head_t. A wait queue head is the handle kernel code uses when a task needs to sleep until something happens. Here is the typedef, simplified from a current kernel:
/* Simplified from the Linux kernel */
struct wait_queue_head {
spinlock_t lock;
struct list_head head;
};
typedef struct wait_queue_head wait_queue_head_t;
That is the same pattern as typedef struct student student_t. The struct keeps a tag, wait_queue_head, and the typedef gives it a short name. In older kernels the tag was __wait_queue_head, and the typedef name was already wait_queue_head_t.
The item on the queue used to have its own typedef, wait_queue_t. Today that struct is struct wait_queue_entry, and the typedef is wait_queue_entry_t. If you read older kernel material and see wait_queue_t, that is the old name.
Notice spinlock_t in the first field. That is another typedef. I will come back to it in a moment. The head field links waiting tasks together. Lists get their own series later, so we will not unpack that here.
Even here, a lot of current kernel code says struct wait_queue_head rather than wait_queue_head_t. The typedef still exists so older code keeps compiling.
Why the Linux kernel typedefs pte_t and pgd_t
Some typedefs in the Linux kernel are not there to save typing. They exist so the compiler will stop you mixing up two values that happen to hold the same kind of number.
The kernel's page tables are a good example. On a typical x86-64 machine, a page table entry and a page global directory entry are both an unsigned long underneath. If they used the same type, you could assign one to the other and the compiler would stay quiet. They are not the same thing, so that assignment should be a mistake.
The kernel wraps each one in its own struct, then typedefs that struct. This is a shortened form of the x86-64 definitions:
/* Simplified from the Linux kernel, x86-64 */
typedef struct { unsigned long pte; } pte_t;
typedef struct { unsigned long pgd; } pgd_t;
pte_t and pgd_t each hold one number, but they are different types. C will not let you assign a pte_t to a pgd_t. The kernel coding style calls types like these opaque. Ordinary code is not supposed to read the field by name. It uses helper functions such as pte_val() and __pte(), or pgd_val() and __pgd(), so you cannot mix them up by accident.
You can see the type check with a tiny program. It prints the two values, and it leaves one bad assignment commented out.
#include <stdio.h>
/* Same idea as the kernel's wrapper typedefs */
typedef struct { unsigned long pte; } pte_t;
typedef struct { unsigned long pgd; } pgd_t;
int main(void)
{
pte_t e = { 0xabc };
pgd_t d = { 0x1000 };
printf("pte=0x%lx pgd=0x%lx\n", e.pte, d.pgd);
/* d = e; */
/* If you uncomment that line, gcc says:
incompatible types when assigning to type 'pgd_t' from type 'pte_t' */
return 0;
}
Save it as pte_pgd.c, compile it with gcc pte_pgd.c -o pte_pgd, and run ./pte_pgd. In PowerShell the run command is .\pte_pgd.exe.
Output:
pte=0xabc pgd=0x1000
I am reading e.pte and d.pgd here only to print them. In real Linux kernel code you would go through the helpers, so most files never mention the field name at all. If those two types had been a plain unsigned long, the assignment d = e would have compiled, and a bug like that could have been hard to spot.
atomic_t and spinlock_t in the Linux kernel
atomic_t and spinlock_t are two more typedefs you will keep seeing in Linux kernel structs. Both are there so that callers use the official helper functions, rather than touching the inside of the type directly.
atomic_t is a counter that the kernel can update without being interrupted halfway through. A simplified form of the current typedef is:
/* Simplified from the Linux kernel */
typedef struct {
int counter;
} atomic_t;
It looks like a struct with one int in it. The point of the typedef is that kernel code should not write v.counter++. A C ++ is not guaranteed to be atomic, so the kernel gives you helpers such as atomic_read(), atomic_set() and atomic_inc(). Those are implemented as single atomic instructions. Ordinary code is meant to go through the helpers, not treat the counter as a plain int.
spinlock_t is a typedef for struct spinlock:
/* Simplified from the Linux kernel */
typedef struct spinlock spinlock_t;
A spin lock is how one CPU holds a short lock while another CPU waits. The inside of struct spinlock changes with kernel config options, so most code never mentions the fields. It uses helpers such as spin_lock() and spin_unlock(). In older kernels you may see field names such as slock. Today you should treat spinlock_t as a type you pass to the helpers, not as a struct you fill in by hand.
Why the Linux kernel mostly avoids typedef
If typedef is so handy, why does the Linux kernel not use it on task_struct, timespec and most of the other structs in this series?
The Linux kernel coding style is direct about this. It says it is a mistake to typedef ordinary structures and pointers. The example in the document is the one I used earlier. vps_t a; hides what a is. struct virtual_container *a; does not. The rule of thumb is: a pointer, or a struct whose fields can reasonably be accessed directly, should never be a typedef.
That is why you write struct task_struct * in kernel code, not task_t or task_ptr. You can see it is a struct, and you can see it is a pointer. The same goes for struct timespec from the last post.
The same document says typedefs are allowed when they fall into a few special cases. The ones that matter for structs are these.
Opaque types, such as pte_t. You are not supposed to look inside. You only use the accessor functions. The coding style mentions pte_t by name as the example.
Wrapper types that exist so the compiler can type-check two values that would otherwise look the same. pte_t and pgd_t are this idea in struct form. The kernel also uses a tool called sparse, which can turn an integer typedef into a distinct type for checking.
Fixed-size integers, such as u32. These are typedefs for integers of a known width, so you are not left wondering whether a value is an int or a long. The coding style also allows the __u32 form in headers that userspace may include, because those headers cannot always use the C99 names or the kernel's u32.
The document is strict about the closing rule: do not add a typedef unless you can clearly match one of those cases. Older kernel code does not always follow today's rule. wait_queue_head_t is a leftover of that kind. Its fields are meant to be used, and current code often writes struct wait_queue_head instead. For new kernel code, and for your own structs as you learn, the safer default is the one we used in the last post: keep the struct tag visible, and add a typedef only when you have a reason like the ones above.
In your own user programs, typedef struct student student_t is still fine. Plenty of good C code does it. Just be careful not to typedef a pointer, and remember that the typedef is only a name. When you start reading Linux kernel sources, expect struct to be spelled out most of the time.
What comes next
We now have two ways to name a struct type: the struct tag, and a typedef. We also know why Linux kernel typedefs such as pte_t and atomic_t keep those short names, and why struct task_struct does not.
In the next post, How a Struct Sits in Memory: Size, Padding and Alignment, we will look at what happens when those fields are laid out in memory, and why a struct is sometimes larger than the sum of its fields.
If you want to practice this with small exercises that check your code as you go, the C programming course at Jupius Learning starts from exactly here.
Frequently asked questions
What does typedef struct do in C?
It gives the struct a second type name. After typedef struct student student_t;, you can write student_t s1; instead of struct student s1;. The typedef does not change the fields, and it does not create a variable on its own.
What is the difference between a struct tag and a typedef name?
The tag is the word after struct, such as student in struct student. The typedef name, such as student_t, is an alias for that type. You can have both, or you can typedef an anonymous struct and keep only the typedef name. A forward declaration needs the tag.
Why can I not use student_t inside its own typedef?
Because the typedef name exists only after the finishing semicolon. Inside the braces, the compiler already knows the tag, so you write struct student *partner, not student_t *partner. If you typedef the tag first, on its own line, then student_t * is fine after that.
Why does the Linux kernel avoid typedef on most structs?
The Linux kernel coding style says a typedef on an ordinary struct or pointer hides what the type is. struct task_struct *p is clear. A name like task_t or task_ptr is not. The kernel keeps typedef for opaque types and for wrappers that help with type checking. It also typedefs fixed-size integers such as u32.
Why does the Linux kernel typedef pte_t, atomic_t and spinlock_t?
So that each one is a distinct type, and so that most code uses helper functions instead of reading the fields directly. pte_t and pgd_t stop you assigning one kind of page-table value to another. atomic_t and spinlock_t steer you toward helpers such as atomic_inc() and spin_lock().
Top comments (0)