error : lvalue required as unary’ &’ operand [closed]

// Online C++ compiler

int main() {
    int a[3]={9,8,7} ;
    std::cout << &(a+1) <<"___" <<&(a[1]);

    return 0;

Why I am not able to get address values. Its showing error.


You are trying to retrieve the pointer of an rvalue, ie the result of the computation (a+1). An rvalue is defined to be whatever has not got an address, so it can be stored somewhere in the registry for the duration of the computation. It would be like saying &1, but of course 1 has no pointer. The & operand can thus only be used on lvalues, ie values which are stored at some memory address.

In your case, a already corresponds to a pointer to a[0], so in order to print the pointer to a[1], you literally just need:

std::cout << a + 1 << std::endl;

Pointer arithmetics will increment the address by x * sizeof(T), and since a is an array of int, it will increment the address value of a[0] by sizeof(int), hence 4.

Leave a Reply

Your email address will not be published. Required fields are marked *