Related Articles

Related Articles

Why C treats array parameters as pointers?
  • Difficulty Level : Easy
  • Last Updated : 28 May, 2017

In C, array parameters are treated as pointers. The following two definitions of foo() look different, but to the compiler they mean exactly the same thing. It’s preferable to use whichever syntax is more accurate for readability. If the pointer coming in really is the base address of a whole array, then we should use [ ].

filter_none

edit
close

play_arrow

link
brightness_4
code

void foo(int arr_param[]) 
{
  
  /* Silly but valid. Just changes the local pointer */
  arr_param = NULL; 
}
  
void foo(int *arr_param) 
{
  
  /* ditto */
  arr_param = NULL; 
}

chevron_right


Array parameters treated as pointers because of efficiency. It is inefficient to copy the array data in terms of both memory and time; and most of the times, when we pass an array our intention is to just tell the array we interested in, not to create a copy of the array.

Asked by Shobhit

References:
http://cslibrary.stanford.edu/101/EssentialC.pdf

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :