C Notes
Understand how arrays are passed to functions in C, array decay to pointers, why size information is lost, and best practices for writing array-handling functions.
One of the most important — and often misunderstood — concepts in C programming is how arrays interact with functions. When you pass an array to a function, you're not actually passing a copy of the entire array. Instead, C passes the address of the first element. This has profound implications for how you write functions that work with arrays.
Array Decay: The Core Concept
When an array name is used in most expressions (including as a function argument), it "decays" into a pointer to its first element. This is not a copy — it's the actual memory address.
This means:
- The function can modify the original array (it has the real address)
- The function does NOT know the array's size
sizeofinside the function gives pointer size, not array size
Basic Syntax: Passing 1D Arrays
There are three equivalent ways to declare a function that accepts an array:
Complete Example
Original: 5 10 15 20 25 Doubled: 10 20 30 40 50
Notice how doubleElements modified the original array in main — this is because the function works with the same memory, not a copy.
The sizeof Problem
This is a classic pitfall that trips up many C programmers:
In main: sizeof(numbers) = 20 Inside function: sizeof(arr) = 8
Inside main, sizeof(numbers) returns 20 (5 integers × 4 bytes). But inside the function, sizeof(arr) returns 8 (the size of a pointer on a 64-bit system). The array size information is completely lost!
Solution: Always pass the size as a separate parameter.
Passing 2D Arrays to Functions
For 2D arrays, you must specify the column count in the parameter:
Using notation 1: 1 2 3 4 5 6 7 8 9 10 11 12 Using notation 2: 1 2 3 4 5 6 7 8 9 10 11 12
Returning Arrays from Functions
You cannot directly return a local array from a function (it would be destroyed when the function ends). Here are valid approaches:
Approach 1: Modify the Array In-Place
Approach 2: Pass a Destination Array
Approach 3: Return Dynamically Allocated Array
Using const for Read-Only Access
When a function should only read an array without modifying it, use const:
Maximum value: 96
Practical Pattern: Array Processing Functions
Here's a well-structured example showing multiple array operations:
Average: 73.25 Above average: 4 students Normalized (0-100 scale): 40 66 94 0 86 56 100 10
Summary Table: Array vs Pointer Parameter
| Feature | Array in main | Array in function |
|---|---|---|
sizeof(arr) | Total array size | Pointer size (4/8 bytes) |
Can use [] indexing | Yes | Yes |
| Modifying elements | Affects local copy | Affects original |
| Knows its own size | Yes (with sizeof trick) | No — must be passed |
&arr type | Pointer to entire array | Pointer to pointer |
Interview Questions
Q1: Why does C not pass arrays by value (copying the entire array)?
For efficiency. Arrays can be very large — copying thousands of elements every time a function is called would waste time and memory. Passing only the base address (a single pointer) is fast regardless of array size.
**Q2: What's the difference between int arr[] and int *arr as function parameters?**
There is no difference at all. Both declare a pointer to int. The arr[] syntax is purely for readability, signaling to the programmer that a pointer to an array is expected. The compiler treats them identically.
Q3: How can you prevent a function from modifying the array it receives?
Use the const qualifier: void func(const int arr[], int size). Any attempt to modify arr[i] inside the function will cause a compile-time error.
Q4: Is it possible to pass an array by value in C?
Not directly. However, you can wrap the array inside a struct and pass the struct by value — this creates a full copy. Example: struct Wrapper { int arr[100]; };
Q5: What happens if you pass NULL as an array argument?
The function will receive a NULL pointer. If it attempts to dereference it (access elements), the program will crash with a segmentation fault. Always validate pointers before use.
Summary
- Arrays decay to pointers when passed to functions — only the base address is transferred
- Size information is lost during decay — always pass size as a separate parameter
- Functions can modify the original array since they receive the real address
- Use
constto enforce read-only access to array parameters - For 2D arrays, all dimensions except the first must be specified in the parameter
- You cannot return a local array — use in-place modification, output parameters, or dynamic allocation
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Passing Arrays to Functions in C.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, arrays, passing, functions, passing arrays to functions in c
Related C Programming Topics