VOOZH about

URL: https://www.geeksforgeeks.org/c/why-c-treats-array-parameters-as-pointers/

⇱ Why Does C Treat Array Parameters as Pointers? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Why Does C Treat Array Parameters as Pointers?

Last Updated : 12 Apr, 2023

In C, array parameters are treated as pointers mainly to,

  • To increase the efficiency of code
  • To 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.


Output
The sum of the array is: 15

The sum of the array is: 15 
Comment
Article Tags: