CLA (C language) study materials
CLA (C language) study resources

CLA (C language): Functions, Arrays, Pointers and Dynamic Memory Handling

CLA (C language) · Work through your exam, one topic at a time.

All guide sections

Functions, Arrays, Pointers and Dynamic Memory Handling

Domain 3 - Functions, arrays, pointers and memory handling is the core of practical C: how arguments reach a function, how arrays relate to pointers, and who owns heap memory. Items test prototypes, array decay, pointer arithmetic and correct malloc/free discipline.

Functions and parameter passing

Every argument is passed by value; the callee receives copies. To modify a caller's object, pass its address and dereference it inside. An array argument decays to a pointer to its first element, so the callee can change elements - but sizeof inside the function gives pointer size, not array size. Prototypes let the compiler check argument counts and types; variadic functions need stdarg.h and at least one named parameter before ....

Arrays

Elements are contiguous and zero-indexed, with no bounds checking. int a[5] = {1, 2}; zero-fills the remainder. int m[3][4] is three contiguous rows of four int, and m[i][j] means *(*(m+i)+j). sizeof a yields the whole array only where the array type is still visible.

Pointers and arithmetic

&x yields an address and *p dereferences it. p + n advances by n * sizeof(*p) bytes. Forming and comparing a one-past-the-end pointer is legal; dereferencing it is not. Strings are char arrays ending in '\0'; strlen counts characters before it while sizeof includes the terminator. void * converts to and from any object pointer without a cast.

Dynamic memory

malloc, calloc (zeroed), realloc and free come from stdlib.h. Check for NULL, size with n * sizeof *p, and free only memory these functions returned, exactly once. realloc may move the block, so store its result in a temporary before overwriting the original pointer. free does not null the pointer.

Worked example

int *p = malloc(n * sizeof *p); if (!p) return -1; for (int i = 0; i < n; i++) p[i] = i; free(p); p = NULL; Writing sizeof *p keeps the allocation correct if the element type changes later, and nulling after free turns accidental reuse into an early crash instead of silent corruption.

Traps

sizeof on a pointer parameter; returning the address of a local array; loops off by one and strcpy without room for '\0'; double free; use-after-free; assuming malloc zeroes memory.