What are typedef Pointers Arrays Functions- C Programming Tutorials For Beginners

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

typedef Pointers Arrays Functions

Lecture 50 of our free C programming course: name pointer, array and function types for cleaner embedded C programming.

Chapter 10

Structure and Union

Level

Intermediate

Reading time

13 minutes

Aliasing int is easy. The real power of typedef in embedded C programming appears when you name pointer, array and function types, because those declarations are the hardest to read in C. A callback table of function pointers, a MAC address array or a register pointer becomes one short, readable name. This lecture of our free C tutorial shows each case with a complete original program, real output, and the traps that catch even experienced engineers. It continues the free embedded C course from the typedef basics lecture.

What You Will Learn

  • How to typedef a pointer type and declare several pointers safely
  • How to typedef an array type and what happens when you pass it to a function
  • How to typedef a function type and a function pointer type
  • How to build a table of callbacks using a typedef
  • Which mistakes to avoid with const, arrays and function types

Prerequisites

Know basic typedef syntax from the previous lecture, plus pointers, arrays and function pointers from earlier chapters of this free C programming course.

The One Rule That Unlocks Everything

To write a typedef for any complicated type, first write an ordinary variable declaration of that type, then put typedef in front. The variable name turns into the type name.

Declaration to typedef in two steps
pointeruint32_t *reg;becomestypedef uint32_t *reg_ptr_t;
arrayuint8_t mac[6];becomestypedef uint8_t mac_t[6];
functionint op(int, int);becomestypedef int binop_t(int, int);
function pointerint (*op)(int, int);becomestypedef int (*binop_ptr_t)(int, int);

Complete Demonstration Program

/* file: typedef_derived.c */
#include <stdio.h>
#include <stdint.h>

typedef uint32_t *reg_ptr_t;
typedef uint8_t   mac_t[6];
typedef int       binop_t(int, int);
typedef int     (*binop_ptr_t)(int, int);

binop_t add_i, mul_i;

int add_i(int a, int b) { return a + b; }
int mul_i(int a, int b) { return a * b; }

static size_t param_size(mac_t m)
{
    return sizeof m;
}

int main(void)
{
    uint32_t fake_regs[4] = { 10, 20, 30, 40 };
    reg_ptr_t p = fake_regs, q = &fake_regs[2], *pp = &p;

    mac_t addr = { 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01 };
    mac_t table[2];

    binop_ptr_t ops[2] = { add_i, mul_i };

    printf("*p=%u *q=%u **pp=%u\n", *p, *q, **pp);
    printf("sizeof(mac_t)=%zu sizeof(table)=%zu sizeof(param)=%zu\n",
           sizeof addr, sizeof table, param_size(addr));
    printf("add=%d mul=%d\n", ops[0](6, 7), ops[1](6, 7));
    return 0;
}
$ gcc -std=c11 -Wall -Wextra -o typedef_derived typedef_derived.c
typedef_derived.c: In function 'param_size':
typedef_derived.c:16:19: warning: 'sizeof' on array function parameter 'm' will return size of 'uint8_t *' [-Wsizeof-array-argument]
$ ./typedef_derived
*p=10 *q=30 **pp=10
sizeof(mac_t)=6 sizeof(table)=12 sizeof(param)=8
add=13 mul=42

The warning is intentional: it is the compiler telling you about the array-parameter trap we explain below. The program output is shown for a 64-bit PC; the last size would be 4 on a 32-bit target.

Pointer typedefs

reg_ptr_t p = fake_regs, q = &fake_regs[2], *pp = &p; declares three variables from one typedef. Because the star lives inside the type name, p and q are both pointers to uint32_t, and pp is a pointer to a reg_ptr_t, that is a pointer to pointer. Dereferencing twice (**pp) reaches the first register value, 10.

Pointer to pointer built from reg_ptr_t
pppoints toppoints tofake_regs[0]value 10
qpoints tofake_regs[2]value 30

The const trap. With typedef int *int_ptr_t;, the declaration const int_ptr_t p; makes p itself constant (a constant pointer to a modifiable int), not a pointer to a constant int. Because the star is hidden inside the name, this surprises many programmers. For memory-mapped registers, write the qualifiers into the typedef deliberately, for example a typedef for volatile uint32_t *.

Array typedefs

typedef uint8_t mac_t[6]; makes mac_t mean “array of six bytes”. A declaration mac_t addr; is a six-byte array, and mac_t table[2]; is an array of two such arrays, twelve bytes in total, as the output confirms. This is neat for fixed-size data such as MAC addresses, UUIDs or keys.

Arrays Still Decay in Parameters

Hiding the array size behind a typedef does not change a C rule: when an array is passed to a function, it is adjusted to a pointer. The function param_size(mac_t m) receives a uint8_t *, so sizeof m reports 8, not 6. If a function needs the length, pass it separately, or wrap the array in a structure.

ExpressionValue on 64-bit PCReason
sizeof(mac_t)6Real array type
sizeof(table)12Two arrays of six bytes
sizeof(param) inside function8Parameter decayed to a pointer

Function Pointer Typedef in C

typedef int binop_t(int, int); names a function type: any function taking two ints and returning an int. You can use it to declare several functions at once: binop_t add_i, mul_i; is the same as two ordinary prototypes. You still write each function body normally; a function type typedef cannot be used to define a body.

More common in practice is the function pointer typedef, binop_ptr_t. Without it, an array of callbacks looks like int (*ops[2])(int, int). With it, the same thing is binop_ptr_t ops[2]. The call ops[1](6, 7) runs mul_i and returns 42.

Callback table using binop_ptr_t
ops[0]points toadd_iops[0](6, 7) gives 13
ops[1]points tomul_iops[1](6, 7) gives 42

Function Pointer Typedef Use Cases

TypedefWhere you will see it
Function pointer typeInterrupt handlers, task entry points, driver callbacks, command tables
Array typeMAC addresses, device IDs, encryption keys, fixed-length buffers
Pointer typeRegister block pointers, buffer pointers, opaque handles

A classic example is an interrupt vector table: an array of void (*)(void) entries. With typedef void (*isr_t)(void); the table is simply isr_t vectors[N], and the code is far easier to review.

Common Mistakes

  • Expecting sizeof on an array parameter to give the array size.
  • Putting const in front of a pointer typedef and assuming it protects the data.
  • Forgetting parentheses around *name in a function pointer typedef; without them you declare a function returning a pointer.
  • Trying to define a function body with a function typedef.
  • Using an array typedef as a return type; C functions cannot return arrays.

Function Pointer Typedef Best Practices

Guidelines

  • Prefer typedefs for function pointers; leave plain data pointers visible with an explicit star unless they are opaque handles.
  • Keep array lengths in a named constant and pass lengths to functions explicitly.
  • Give function pointer typedefs descriptive suffixes such as _cb or _fn so readers know they are callable.
  • Always check function pointers for NULL before calling them in driver code.

Performance and Security Considerations

A call through a function pointer is an indirect branch, usually a couple of cycles slower than a direct call and impossible to inline. In a tight inner loop that matters; in an event dispatcher or a driver callback it does not. Measure before you decide to avoid them.

From a security point of view, function pointer tables are attractive targets: if a buffer overflow overwrites a pointer in RAM, the attacker chooses where your program jumps. Keep dispatch tables const so they live in read-only flash, validate any index used to select an entry, and check pointers for NULL before calling.

Practice Exercise

Extend the demo with sub_i and div_i, add them to the ops table, and loop over the table to print all four results for the inputs 20 and 4. Guard the division against a zero divisor. Then make the table static const and notice what the compiler now requires from the declaration.

Summary and Key Takeaways

  • Write the declaration first, add typedef in front, and the variable name becomes the type name.
  • A pointer typedef lets one statement declare several pointers correctly, but it hides the star and can confuse const.
  • An array typedef keeps its size for variables, yet parameters of that type still decay to pointers.
  • A function pointer typedef turns unreadable callback declarations into one short name; this function pointer typedef habit is everywhere in firmware.

Frequently Asked Questions

How do I write a typedef for a function pointer?

Write the declaration, for example int (*op)(int, int);, and put typedef in front: typedef int (*op_t)(int, int);.

What is the difference between a function type and a function pointer typedef?

A function type names the function itself and is mostly used to declare prototypes. A function pointer typedef names a pointer to such a function and is used for variables, arrays and parameters.

Can I typedef an array in C?

Yes, such as typedef uint8_t mac_t[6];. Variables of that type are real arrays.

Why does sizeof give the pointer size for my array parameter?

Array parameters are adjusted to pointers by the language rules. Pass the length as another argument.

Does typedef of a pointer make const behave differently?

Yes. const applies to the whole pointer type, so it makes the pointer constant, not the pointed-to data.

Where are function pointer typedefs used in firmware?

In interrupt vector tables, callback registration, command dispatch tables and RTOS task creation.

Free Embedded C Course on EmbeddedPathashala

Continue with typedef and structures, then finish the chapter with a quiz and interview questions.

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

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

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →