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

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 first time is normal
C's "array decays to a pointer" rule is counter-intuitive at first.
How to retry
  1. Revisit arrays and pointers
  2. Experiment: inside a function sizeof(a) gives pointer size
  3. Rule: always pass the length as another argument
  4. Defer 2D arrays; master 1D first
πŸ’‘ Tip: In void f(int a[], int n), int a[] is just another spelling of int *a. The callee cannot see the total array size.

Passing an Array to a Function

Passing an array to a function uses a slightly special 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 the array size on its own, so the standard convention is to pass the size as another argument.

Pass-by-Value vs Pass-by-Reference

Plain int/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)
Modifying the parameter inside the function does not affect the caller (only the copy is changed).
Passing an array
Because the address is passed, writing 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

You cannot use sizeof(a) inside the function! Inside the function, the array parameter is actually a pointer, so sizeof returns only the size of a pointer (for example 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
Click Run...
πŸ’‘ Try these ideas too
Advertisement

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 23: Global Variables
Next lesson →
Lesson 25: Pointer Basics

Quick 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 address is what the function receives.

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 too.

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 reaches the function, so sizeof cannot recover the original length β€” pass the count as another argument.

Share this article
Share on X Share on Facebook