Friday, November 29, 2013

Why C treats array parameters as pointers?

In C we can see that array parameters are generally treated as pointers. See the following definitions of two functions:

void function1(int arr[])
{
  /* Silly but valid. Just changes the local pointer */
  arr = NULL;
}
void function2(int *arr)
{
  /* ditto */
  arr = NULL;
}
Array parameters are treated as pointers because of efficiency. Mostly when we pass array to the function then we mostly want to deal with same array. So no need to copy whole array to the function, just reference is sufficient. It is inefficient to copy same array as memory and time perspective .
C language

0 comments:

Post a Comment