Why Does C Treat Array Parameters as Pointers? Last Updated : 12 Apr, 2023 Comments Improve Suggest changes Like Article Like Report In C, array parameters are treated as pointers mainly to, To increase the efficiency of codeTo save time It is inefficient to copy the array data in terms of both memory and time; and most of the time, when we pass an array our intention is to just refer to the array we are interested in, not to create a copy of the array. The following two definitions of fun() look different, but to the compiler, they mean exactly the same thing. void fun(int arr[]) { // body } // This is valid void fun(int *arr) { // body } // This is valid too It's preferable to use whichever syntax is more accurate for readability. Note: If the pointer coming in really is the base address of a whole array, then we should use [ ]. Example: In this example, the array parameters are being used as pointers. C // C Program to demonstrate that C treats array parameters // as pointers #include <stdio.h> void findSum1(int arr[]) { int sum = 0; for (int i = 0; i < 5; i++) sum = sum + arr[i]; printf("The sum of the array is: %d\n", sum); } void findSum2(int* arr) { int sum = 0; for (int i = 0; i < 5; i++) sum = sum + arr[i]; printf("\nThe sum of the array is: %d \n", sum); } // Driver code int main() { int arr[5] = { 1, 2, 3, 4, 5 }; findSum1(arr); findSum2(arr); return 0; } C++ // C++ Program to demonstrate that C treats array parameters // as pointers #include <iostream> using namespace std; void findSum1(int arr[]) { int sum = 0; for (int i = 0; i < 5; i++) sum = sum + arr[i]; cout <<"The sum of the array is: " << sum; } void findSum2(int* arr) { int sum = 0; for (int i = 0; i < 5; i++) sum = sum + arr[i]; cout <<"\nThe sum of the array is: "<< sum; } // Driver code int main() { int arr[5] = { 1, 2, 3, 4, 5 }; findSum1(arr); findSum2(arr); return 0; } // this code is contributed by shivanisinghss2110 OutputThe sum of the array is: 15 The sum of the array is: 15 Comment More infoAdvertise with us Next Article Why Does C Treat Array Parameters as Pointers? K kartik Follow Improve Article Tags : C Language pointer C-Pointers Similar Reads How to print size of array parameter in C++? How to compute the size of an array CPP? C++ // A C++ program to show that it is wrong to // compute size of an array parameter in a function #include <iostream> using namespace std; void findSize(int arr[]) { cout << sizeof(arr) << endl; } int main() { int a[10]; cout << si 3 min read How to pass a 2D array as a parameter in C? A 2D array is essentially an array of arrays, where each element of the main array holds another array. In this article, we will see how to pass a 2D array to a function.The simplest and most common method to pass 2D array to a function is by specifying the parameter as 2D array with row size and co 3 min read When do we pass arguments by pointer? In C, the pass-by pointer method allows users to pass the address of an argument to the function instead of the actual value. This allows programmers to change the actual data from the function and also improve the performance of the program. In C, variables are passed by pointer in the following ca 5 min read Do Not Use sizeof For Array Parameters in C Using sizeof directly to find the size of arrays can result in an error in the code, as array parameters are treated as pointers. Consider the below program. C // C Program to demonstrate incorrect usage of sizeof() for // arrays #include <stdio.h> void fun(int arr[]) { int i; // sizeof should 4 min read Pointer to an Array | Array Pointer A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; 5 min read Like