Why does C treat array parameters as pointers?
Introduction
An array is a data structure used to store multiple values in a single variable instead of declaring separate variables for each value. A variable stores the memory address of another variable as its value, called a pointer. A pointer variable points to a data type (like int ) of the same type and is created with the * operator. In this article, we will discuss why C treats array parameters as pointers.
You can also read about the dynamic arrays in c.
Why does C treat array parameters as pointers?
In the programming language of C, it has been observed that it treats array parameters as pointers. In C, the array name acts as a pointer and can be passed to function. When an array is passed as a parameter to a function, it doesn’t create a copy of the array. Rather the array parameter/ array name acts as a pointer that points to the base address or the first element, i.e., the element present at the 0th index of the array. This feature in C increases the coding efficiency and saves time and space. In most cases, the passing of an array to a function is done so that the function can access and modify the array elements in place without creating a copy of the whole array, which is inefficient and takes extra space and time.
void ninja(int arr[]) {
}
void ninja(int *arr) {
}
In the above code, both the function definitions are valid, and in both cases, the array parameters behave like a pointer.
Take, for example:
void printEven1(int arr[]){
int i;
printf("\n Even elements in the array are: \n");
for(i=0;i<6;i++)
printf(" %d",a[i]);
}
void printEven2(int *arr){
int i;
printf("\n Even elements in the array are: \n");
for(i=0;i<6;i++)
printf(" %d",*(arr+i));
}
void main(){
int a[5] = {1,2,3,4,5,6};
printEven1(arr);
printEven2(arr);
}
Output
Even elements in the array are:
2 4 6
Even elements in the array are:
2 4 6
You can implement it by yourself on c online compiler.
Frequently Asked Questions
Are arrays considered pointers in C?
An array is a pointer, and you can store that pointer into any pointer variable of the correct type. For example, int A[10]; int* p = A; p[0] = 0; makes variable p point to the first member of array A.
Why are arrays treated as pointers in C?
In C, array parameters are treated as pointers mainly to increase the efficiency of code and also to save time.
What is an array in the C programming language?
An array is a data structure used to store multiple values in a single variable instead of declaring separate variables for each value.
Conclusion
In this article, we have extensively discussed why C treats array parameters as pointers. I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here are some similar blogs to redirect: What are arrays in C?, Arrays and Pointers, Introduction to Pointers, Array of pointers. We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Here are some courses provided by Coding Ninjas: Basics of C++ with DSA, Competitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.
Happy Learning!