Reading history

typedef in C: Aliases for Structs, Arrays and Function Pointers

4 min read 2 views

Learn typedef in C: creating type aliases, typedef with structs and self-referencing structs, function pointer typedefs, standard typedefs, and typedef vs #define.

The typedef keyword gives a new name to an existing type. It does not create a new type; it creates an alias that is shorter, clearer or easier to change later. C programmers use it everywhere: for structures so they can write Student instead of struct Student, for fixed-size integers like uint32_t, and above all for function pointer types, whose raw syntax is hard to read.

In this lesson you will learn

  • The syntax typedef existing_type new_name;
  • Aliases for basic and array types
  • typedef with structures, including self-referencing ones
  • Function pointer typedefs
  • typedef vs #define

typedef in Action

#include <stdio.h>

typedef unsigned long long u64;               /* shorter name for a long type */
typedef int Marks[5];                         /* an array type */

typedef struct {
    char name[20];
    float cgpa;
} Student;                                    /* no need to write "struct" each time */

typedef struct Node {                         /* self-referencing struct needs a tag */
    int value;
    struct Node *next;
} Node;

typedef int (*Operation)(int, int);           /* pointer-to-function type */

static int add(int a, int b) { return a + b; }
static int mul(int a, int b) { return a * b; }

int main(void) {
    u64 big = 18446744073709551615ULL;
    printf("u64 max: %llu (%zu bytes)\n", big, sizeof(u64));

    Marks m = {78, 91, 64, 88, 70};
    printf("third mark: %d, array size: %zu bytes\n", m[2], sizeof(Marks));

    Student s = {"Nadia", 3.82f};
    printf("%s has CGPA %.2f\n", s.name, s.cgpa);

    Node second = {20, NULL};
    Node first = {10, &second};
    printf("list: %d -> %d\n", first.value, first.next->value);

    Operation ops[] = {add, mul};
    const char *names[] = {"add", "mul"};
    for (int i = 0; i < 2; i++)
        printf("%s(6, 7) = %d\n", names[i], ops[i](6, 7));
    return 0;
}

Output

u64 max: 18446744073709551615 (8 bytes)
third mark: 64, array size: 20 bytes
Nadia has CGPA 3.82
list: 10 -> 20
add(6, 7) = 13
mul(6, 7) = 42
  • typedef unsigned long long u64; reads like a variable declaration with typedef in front: the name in the variable position, u64, becomes the alias.
  • The same rule works for arrays: typedef int Marks[5]; makes Marks mean “array of 5 ints”.
  • typedef struct { ... } Student; lets you declare Student s; instead of struct Student s;. See Structures in C.
  • A structure that points to its own type, like a linked-list Node, needs a tag (struct Node) because the alias does not exist yet inside the braces.
  • typedef int (*Operation)(int, int); names the type “pointer to a function taking two ints and returning an int”. An array of such pointers, Operation ops[], is now easy to read. Compare the raw form int (*ops[])(int, int). See Function Pointers.

typedef vs #define

typedef#define
Handled byThe compilerThe preprocessor (text replacement)
Follows scope rulesYesNo
Works with pointersCorrectlyCan surprise you
Can alias arrays and function typesYesNot cleanly
#define PTR_D int*
typedef int* PTR_T;

PTR_D a, b;   /* expands to: int* a, b;  so b is a plain int! */
PTR_T c, d;   /* both c and d are int*                        */

typedefs in the Standard Library

AliasHeaderMeaning
size_t<stddef.h>Unsigned type for sizes and counts (returned by sizeof)
uint8_t, int32_t, uint64_t<stdint.h>Integers of exact width
FILE<stdio.h>A file stream structure
time_t<time.h>Calendar time

Common Mistakes

  1. Thinking typedef creates a new, incompatible type; it is only another name, so values mix freely.
  2. Using the alias inside its own struct definition; use the struct tag there.
  3. Hiding pointers behind typedefs (typedef Student *StudentPtr;), which makes it unclear that a variable is a pointer. Many style guides avoid this except for function pointers.
  4. Using names ending in _t for your own types; POSIX reserves them.

Practice Exercises

Exercise 1. Create a typedef Point for a struct with x and y doubles, and a function that returns the distance between two points.

Show answer
#include <math.h>
typedef struct { double x, y; } Point;

double distance(Point a, Point b) {
    return hypot(a.x - b.x, a.y - b.y);
}

Exercise 2. Write a typedef for a pointer to a function that takes a const char * and returns nothing.

Show answer
typedef void (*Logger)(const char *message);

Frequently Asked Questions

Does typedef make my program slower or larger?

No. It exists only at compile time; the generated machine code is identical to code written with the original type.

Should every struct have a typedef?

It is a style choice. Many C projects typedef their structs; the Linux kernel style guide prefers writing struct name explicitly so the reader always knows it is a structure.

Summary

  • typedef creates an alias for an existing type.
  • It shortens struct names and makes function pointer types readable.
  • Self-referencing structs still need a tag.
  • Prefer typedef to #define for types.

Next: const and volatile in C.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *