2

In the below code-

(Consider this codes is enclosed in the main function with all the necessary headers)

int arr[5] = {10,20,30,40,50};

cout << &(arr);

cout << &(arr+1);

If we just keep the first cout it works and prints the starting address of the array.

But if we keep the second cout it gives compilation error.

Why does it behaves in this manner?

Christophe
  • 68,716
  • 7
  • 72
  • 138
pranav
  • 163
  • 1
  • 3
  • 12
  • 3
    @pmg Are you saying `&(arr + 1)` should compile in C, or simply that one needs `printf` to print stuff in C? – HolyBlackCat Feb 24 '19 at 22:17
  • 1
    *"if we keep the second cout it gives compilation error."* - Are you saying the reported error was completely unintelligible ? When posting questions about errors, especially compilation errors, **always** post the error message *verbatim*, [within your question](https://stackoverflow.com/posts/54857020/edit) and what you think it means, or what parts of it you're having trouble understanding as they relate to your code. – WhozCraig Feb 24 '19 at 22:26

2 Answers2

1

Because & is taking the address of an lvalue, i.e. an object.

arr is an lvalue that corresponds to the array. This is why the first works. But arr+1 isn't. It's a temporary result (which by the way corresponds already to an address).

If you want to get the address, without compilation error, you can use one of the following:

cout << arr+1 <<endl;      // get address directly using pointer maths
cout << &arr[1] <<endl;    // gets address of an element in the array
cout << &*(arr+1) <<endl;  // long and painful:  transform an address in a pointr
                           // and back again.  Better use the first alternative

Here an online demo. By the way, the first can be simplified to cout<<arr<<endl;

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • note that [`'\n'` should be preferred to `endl`](https://stackoverflow.com/q/213907/995714) – phuclv Feb 25 '19 at 00:22
1

Why does it behaves in this manner?

Adding an integer to a pointer is an expression that results in a new value. The value category of the expression is rvalue.

The operand of the address-of operator must be an lvalue. Rvalues are not lvalues. You cannot take the address of an expression that returns a new value.


It is somewhat unclear what you're trying to do. Here are some examples of expressions:

&(arr[0])   // address of the first element
arr + 0     // same as above

&(arr[1])   // address of the second element
arr + 1     // same as above

&arr        // address of the array.
            // note that type of the expression is different,
            // although the value is same as the first element

(&arr) + 1  // address of the next array (only valid if arr is
            // a subarray within a multidimensional array
            // which is not the case in your example)

&(arr+1)    // ill-formed; has no sensical interpretation

arr is not a pointer; it is an array. But arrays decay to a pointer to first element in expressions that use the value, so in this context the type of the expression is indeed a pointer after the array-pointer conversion.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Can you elaborate on 'arr is not a pointer' and 'arrays decay to a pointer'? I've always just thought an array is a way of initializing multiple sequential pointers. Am I wrong? – Bas in het Veld Feb 24 '19 at 22:25
  • 2
    @BasinhetVeld I'm pretty sure that you're wrong, although I don't quite even understand what you mean by *"initializing multiple sequential pointers"*. Arrays and pointers are distinct things with different properties. First is a sequence of objects in consequetive memory locations; Latter is an object that points to another object. The array-pointer decay does in many cases make the array name behave in many cases much like a pointer does because what you have after the conversion is indeed a pointer. – eerorika Feb 24 '19 at 22:35
  • @errorika What I mean (but didn't word very well) is that a pointer like: int* p = (arr + 0); Can be used very much like the arr variable - p[0], (p + 0) does the same thing. But that's exactly because of the decaying. Thanks for the clarification – Bas in het Veld Feb 26 '19 at 08:58