liveBook · Manning

Array parameter

As explained in Modern C

An array parameter is a function parameter declared with array syntax whose array type is adjusted for parameter passing and whose argument is passed as a pointer to the array's first element.

void swap_double(double a[static 2]) declares an array parameter, but C adjusts that parameter to a pointer for the function interface. The call receives access to the original array rather than a copied array. section 6.1.4 section 11.3.2

Keep these rules distinct:

  • The innermost array dimension is lost, so do not use sizeof on an array parameter to determine its array size. section 6.1.4
  • Any array parameter rewrites to a pointer; array notation and equivalent pointer notation describe the same function interface. section 11.3.2
  • For multidimensional arrays, only the innermost dimension is rewritten; the remaining dimensions stay part of the pointed-to array type. section 11.3.2

Changing the caller's array

double A[2] = { 1.0, 2.0 }; swap_double(A); printf("A[0] = %g, A[1] = %g\n", A[0], A[1]); The function exchanges A[0] and A[1]. It operates through the adjusted pointer, so no array copy is involved.

How Modern C covers it

How deep it goes
Defines pointer rewriting, lost innermost dimensions, and multidimensional equivalence, but defers broader pointer mechanics.
Examples
Uses an element-swapping function, string-function pointer declarations, array copying, and matrix multiplication.
What it assumes
Assumes basic function declarations and arrays, while developing the array-pointer relationship and parameter rewriting explicitly.
What you can do after
Apply it by declaring array-shaped interfaces, choosing equivalent pointer notation, and handling multidimensional parameters correctly.
How it connects
Connects arrays with pointers, array decay, multidimensional arrays, variable-length arrays, and function parameters.

Also explained in

Compare how 2 books explain array parameter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.