Skip to study material
← Question index

QUESTION 22

Explain typedef and its use with structures and pointers.

5 marks

Answer in brief

typedef gives a type a convenient alias that can make declarations easier to read.

Examples

typedef unsigned long Count;
typedef struct { int x, y; } Point;
Point p = {2, 3};

Working

Count is an alias for unsigned long. Point names the stated structure type, so Point p declares an object without repeating struct before the type name. The typedef declaration itself is not the object p and does not store a coordinate.

Advantages

  1. Give a meaningful role name to a suitable underlying type.
  2. Simplify repeated structure declarations.
  3. Centralise a representation choice when interfaces consistently use the alias.
  4. Make complex function-pointer declarations easier to manage.

Pointer aliases

typedef int *IntPointer; makes both a and b pointers in IntPointer a, b;. This differs from int *a, b;, where only a is a pointer. However, hiding a pointer can also obscure const behaviour: const IntPointer qualifies the pointer object, not the pointed-to int. Keep aliases understandable.

Important distinction

typedef is not a preprocessor #define. It participates in C’s declaration and type rules rather than merely replacing tokens. It does not automatically change a data structure’s runtime performance or allocate new storage. A type alias can improve readability, but the underlying type’s size, range and operations still apply.