Home > Software engineering >  Is there any difference in passing &arr(address of entire block) or just passing name of array (addr
Is there any difference in passing &arr(address of entire block) or just passing name of array (addr

Time:12-31

void func1(int* ptr)
{
    printf("func1 :%d\n",  ptr);
}

int main()
{
    int arr[4] = {0,1,2,3};
    printf("Addr enter code here`f arr: %d\n",arr);
    func1(arr);  // first way:
    func1(&arr); // second way: How this will be different from func1(arr). 
}

[Image showing code run][1] [1]: https://i.stack.imgur.com/Prvii.png

CodePudding user response:

The difference between &arr and arr is that the type of former is a pointer to an array int (*)[4], and the type of latter is an array int[4] which can decay to pointer to the first element int*. Both the pointer to the array, and the decayed pointer to the first element point to the same address because the first byte of the array is also the first byte of the first element of the array.

The difference between func1(&arr) and func1(arr) is that former is ill-formed because int (*)[4] doesn't implicitly convert to int*. In order to be able to call func1(&arr), you would have to accept a pointer of correct type:

void func1(int (*ptr)[4])

printf("func1 :%d\n",  ptr);
printf("Addr enter code here`f arr: %d\n",arr);

Both of these calls result in undefined behaviour. The type of the variadic argument must match the type required by the format specifier. You used the format specifier %d which requires the argument to be of type int (or similar). The type of the (decayed) argument here is int* which isn't int.

CodePudding user response:

Arrays decay to pointer. Any of those pointers reference the same address in memory. The difference is in the type of this pointer.

int arr[5];
  1. arr and &arr[0] have type pointer to int (int *)
  2. &arr has type of pointer to five elements integer array ( int (*)[5])

CodePudding user response:

We have a function named func1 that accepts a pointer to an int that is, its parameter is int*. Now lets see what is happening in each of the cases.

Case I

In this case we have func1(arr);

Here the variable arr is an array of size 4 with elements of type int (which is int [4]) but it decays to a pointer to the first element(which is int*) due to type decay.

So when you wrote,

func1(arr);

essentially you're passing an int* as an argument which matches with the type of the function parameter.

Case II

In this case we have func1(&arr);

Here &arr means a pointer to an array of size 4 with elements of type int.(which is int (*)[4]).

Now when you wrote,

func1(&arr);

essentially you're passing an int (*)[4] as an argument to the function func1. But note that the parameter of func1 is of type int*. So there is mismatch in type of argument and parameter.

Now in the screenshot you posted, the program seems to be compiling and executed successfully. But note that since in the second case there is mismatch in argument and parmeter type you will get undefined behavior.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely on the output of a program that has undefined behavior.


1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

  • Related