I have an array of 5 integers. arr and &arr is same address. So why number 2 gives compilation error and and 3 works fine.
int arr[5] = {1,2,3,4,5};
1. int *p = arr;
2. int (*p1)[5] = arr //Compilation Error
3. int (*p1)[5] = &arr; //Works fine.
arr = 0x61fdf0 and &arr= 0x61fdf0
The problem is that the initialized object and the initializer have different pointer types and there is no implicit conversion from one pointer type to another.
In this declaration
the initialized object has the type
int ( * )[5]while the initializer has the typeint *due to the implicit conversion of the array designator to a pointer to its first element.You have to write
Or for example
Here is a demonstrative program.
The program output is
Objects can have equal values but different types.
Consider an another case.
Let's assume that you have an object of a structure type
in this case
&aand&a.xhave the same values but the types of the expressions are different. You may not write for examplethe compiler will issue an error. But you can write
or
Here is a demonstrative program.
The program output is
As for your one more question in a comment
then in this expression with the operator new
there is allocated memory for an array with 5 elements of the type
int *. The expression returns a pointer to the first element of the array. A pointer to an object of the typeint *(the type of elements of the allocated array) will have the typeint **.