C Reference ยท Functions and Scope
Array parameters in C
Reference for passing arrays to C functions: decay to pointers, why sizeof fails, passing lengths, 2D arrays, static array sizes and pointer-to-array types.
You cannot pass an array to a C function by value. When an array name appears in a function call, it is converted to a pointer to its first element, which is called decay. A parameter written as int a[10] or int a[] is not an array at all: the compiler treats it as int *a.
The consequence is that the function does not know how long the array is. Pass the length as a separate parameter, or use a sentinel value such as the terminating '\0' of a string.
int sum(const int a[], size_t n); /* same as const int *a */ int values[] = {1, 2, 3}; int s = sum(values, 3);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
void f(int a[]); /* adjusted to int *a */ void f(int a[10]); /* still int *a; the 10 is ignored */ void f(int a[static 10]); /* caller promises at least 10 valid elements */ void f(const int a[const]); /* the pointer itself is const (qualifier inside []) */ void f(int (*a)[10]); /* pointer to an array of exactly 10 ints */ void f(size_t n, int a[n]); /* VLA parameter: size given by an earlier parameter */
| Name | Description |
|---|---|
a[] or a[N] |
Adjusted to a pointer to the element type. N has no effect, except with static. |
a[static N] |
Tells the compiler the pointer is non-null and addresses at least N elements. Compilers may warn about short arrays; violating it is undefined. |
(*a)[N] |
A pointer to a whole array. sizeof *a is N elements, and a can be passed as &array. |
m[][COLS] |
A multi-dimensional array parameter; only the first dimension may be omitted. |
Errors / undefined behavior: reading or writing past the real length of the array is undefined. sizeof param yields the size of a pointer, and compilers warn when you divide it by the element size (-Wsizeof-array-argument). Modifying elements through const parameters is a compile error.
Usage notes
- Pass the length alongside the pointer. Put the size first when you want to use it in a VLA-style parameter (
size_t n, int a[n]). VLA parameters are optional in C11 and later but are accepted by GCC and Clang. - Const correctness. Use
const int *for input arrays. A non-const array can be passed whereconstis expected without a cast. - Functions modify the original. The elements are not copied, so
a[i] = 0changes the caller's array. Wrap an array in a struct to pass a real copy. - 2D arrays need the column count in the type, because the compiler computes
m[r][c]asr * COLS + c.int **is a different type and does not acceptint[2][3]. - Strings are arrays of
charending in'\0', so most string functions need no length: they scan for the terminator. - Subarrays.
a + kpasses the tail starting at indexk; combine with a smaller count. - Compound literals can be passed as temporary arrays:
sum((int[]){1, 2, 3}, 3). - Returning arrays is not possible either; return a pointer to allocated or static memory, or fill a caller-provided buffer. See return values.