Address of operator and r-value

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])

Then, the address of operator (&) has (a + 1) operand but (a + 1) is an r-value expression and therefore &a+1 should cause a compiler error.

No, the & operator is applied to a not to a + 1. &a + 1 === (&a) + 1

In your case &a is a pointer to 5 int elements. &a + 1 references the next array of 5 int elements, not the next element of the array a. Then you assign this value to int * pointer and derefence the previous int value. As pointer ptr is referencing the element one past the last element of the the array a so the previous element is the last element of the array a

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top