πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

Arrays as Function Arguments

How to pass arrays to C functions, and how they relate to pointers.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • Passing an array decays to a pointer
  • You must pass the length separately
  • sizeof in the callee gives pointer size, not array size
⭐ Read if you have time
  • VLA parameters int a[n] (C99)
  • For 2D arrays fix the column count
  • Use const to mark read-only
πŸ’ͺ Not getting it on the first try? That's normal.
C's "array decays to a pointer" rule feels unintuitive at first.
How to approach it again
  1. Revisit arrays and pointers
  2. Try it yourself: inside a function sizeof(a) gives the pointer size
  3. Rule of thumb: always pass the length as a separate argument
  4. Skip 2D arrays for now β€” master 1D first
πŸ’‘ Tip: in void f(int a[], int n), int a[] is just another way to spell int *a. The callee can't see the original array size.

Passing an Array to a Function

Passing an array to a function uses a slightly unusual parameter syntax.
// Two equivalent parameter forms
int sumArray(int a[], int n) { ... }
int sumArray(int *a, int n) { ... }

int main(void) {
  int scores[5] = {80,65,92,78,88};
  int total = sumArray(scores, 5); // pass the array name alone (no &)
  printf("%d\n", total);
}

int sumArray(int a[], int n) {
  int s = 0;
  for(int i=0; i<n; i++) s += a[i];
  return s;
}
Array size: the function has no way to know how many elements were passed, so the standard convention is to pass the size as an extra argument.

Passing Values vs Passing Array Addresses

Plain types like int and double are passed as a copy of the value, but arrays are passed as the address of the first element.
Regular pass-by-value (int, double)
Changes to the parameter inside the function don't affect the caller β€” only the copy is modified.
Passing an array
Since the address is what's passed, a[i] = 0 inside the function also changes the caller's array!
void reset(int a[], int n) {
  for(int i=0; i<n; i++) a[i] = 0; // the caller's array becomes 0 too
}

int main(void) {
  int x[3] = {1,2,3};
  reset(x, 3);
  printf("%d %d %d\n", x[0], x[1], x[2]); // β†’ 0 0 0
}

The sizeof Pitfall

Don't use sizeof(a) inside the function! Inside the function, the array parameter is really a pointer, so sizeof just gives you the size of a pointer (typically 8 bytes).
int main(void) {
  int a[5];
  printf("%ld\n", sizeof(a));       // 20 (5 elements Γ— 4 bytes)
  test(a);
}
void test(int a[]) {
  printf("%ld\n", sizeof(a));       // 8 (size of a pointer)!
}
Workaround: always pass the size as a separate argument, or share it as a constant via a macro / #define.

Try It Yourself β€” Array as Argument

arr_param.c
Output
Press Run…
πŸ’‘ Try these ideas too

Related lessons

Loops, Arrays, Strings
Arrays
Declaring, initializing, and accessing C arrays with memory diagrams.
Advanced
Pointer Basics
Understand pointers with memory visualization.
Functions
Function Basics
Defining and calling C functions, plus parameters and return values.
← Previous lesson
Lesson 25: Global Variables
Next lesson →
Lesson 27: Pointer Basics

Review Quiz

Check your understanding of this lesson.

Q1. What is actually passed when you pass an array to a function?

A copy of all the elements
A pointer to the first element
The size of the array

In C, the array name decays to a pointer to its first element, so the function actually receives an address.

Q2. What happens if you modify array elements inside a function?

The caller's array is modified too
The caller's array is unchanged
Compile error

Because the function works through a pointer, writing to the elements mutates the caller's array as well.

Q3. What is the correct way to tell a function the array length?

It can figure it out with sizeof automatically
Pass the length as a separate argument
You don't need to pass it

Only a pointer makes it into the function, so sizeof can't recover the original length β€” pass the count as a separate argument.

Share this article
Share on X Share on Facebook