Typedef
1. Core Concept
-
Definition: typedef stands for "Type Definition".
-
Function: It does not create a new data type. Instead, it creates a new alias (name) for an existing data type.
-
Purpose: To improve code readability, simplify complex declarations, and enhance portability.
2. Common Usage Patterns
A. Basic Types
Giving semantic meaning to primitive types.
typedef unsigned char byte;
typedef long long timestamp_t;
byte data = 0xFF; // Easier to understand than 'unsigned char'
B. Structures (Most Common)
Avoids repetitive use of the struct keyword.
// Method 1: Anonymous Struct (Recommended)
typedef struct {
int x;
int y;
} Point;
Point p1; // No need to write 'struct Point p1;'
// Method 2: Recursive Structs (Linked Lists)
// Must declare the tag name 'Node' to refer to itself inside
typedef struct Node {
int data;
struct Node* next;
} Node;
C. Arrays
Defining a specific array type (often overlooked).
// 'Arr5' is an alias for "an array of 5 integers"
typedef int Arr5[5];
Arr5 list; // Equivalent to: int list[5];
Arr5 matrix[10]; // Equivalent to: int matrix[10][5];
D. Pointers
Hiding the pointer asterisk * (common in API design for "Handles").
typedef int* IntPtr;
IntPtr ptr = &x;
E. Function Pointers (The Life Saver)
Simplifies the syntax for function pointers significantly.
-
Without typedef (Hard to read):
c void (*signal(int, void (*)(int)))(int); -
With typedef (Clean):
```c // Define a function pointer type named 'Handler' typedef void (*Handler)(int);
// Usage Handler signal(int, Handler); ```
3. Deep Dive: Compiler Principles
A. Compilation Phase
Unlike #define (which is handled by the Preprocessor via text replacement), typedef is handled by the Compiler during the semantic analysis phase.
-
Symbol Table: The compiler adds the alias to its internal symbol table and understands the types compatibility.
-
Scope: It obeys C scoping rules (block scope vs. file scope).
B. Storage Class Specifier
Grammatically, C classifies typedef as a Storage Class Specifier, grouping it with extern, static, auto, and register.
-
Rule: A declaration can have only one storage class specifier.
-
Implication: You cannot combine typedef with static.
c typedef static int MyInt; // ERROR: Multiple storage classes
C. The const Trap (Critical)
This is the most dangerous nuance when using typedef with pointers.
Scenario:
typedef char* PCHAR;
const PCHAR p;
-
Intuition (Wrong): You might think this expands to const char* p (Pointer to a constant character).
-
Reality (Right): It is treated as char* const p (Constant pointer to a mutable character).
Explanation:
The compiler treats PCHAR as a single atomic unit (a pointer type). When you apply const, it makes that unit constant.
- const (char*) : The pointer itself is constant.