Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Why C treats array parameters as pointers?
In C, array parameters are automatically converted to pointers when passed to functions. This behavior is a fundamental design decision that improves memory efficiency and prevents unnecessary copying of large data structures.
Syntax
void function(int arr[]); // Array parameter syntax void function(int *arr); // Equivalent pointer syntax void function(int arr[10]); // Size is ignored
Why Arrays Become Pointers
When you pass an array to a function, C automatically passes the address of the first element instead of copying the entire array. This approach offers several advantages −
- Memory Efficiency: No memory is wasted copying large arrays
- Performance: Passing a pointer is faster than copying thousands of elements
- Consistency: All arrays are passed the same way regardless of size
Example: Array Parameter vs Pointer Parameter
Both function declarations below are functionally identical in C −
#include <stdio.h>
void display1(int a[]) { // Array parameter notation
int i;
printf("Using array notation:<br>");
for(i = 0; i < 5; i++)
printf(" %d", a[i]);
printf("<br>");
}
void display2(int *a) { // Pointer parameter notation
int i;
printf("Using pointer notation:<br>");
for(i = 0; i < 5; i++)
printf(" %d", *(a+i));
printf("<br>");
}
int main() {
int a[5] = {4, 2, 7, 9, 6};
display1(a); // Passes address of first element
display2(a); // Same as above
return 0;
}
Using array notation: 4 2 7 9 6 Using pointer notation: 4 2 7 9 6
Important Considerations
Since arrays decay to pointers, the function cannot determine the array size. You must pass the size separately −
#include <stdio.h>
void printArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("<br>");
}
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
printArray(numbers, size);
return 0;
}
10 20 30 40 50
Conclusion
C treats array parameters as pointers to optimize memory usage and performance. This design choice prevents costly array copying while maintaining efficient access to array elements through pointer arithmetic.
