0

Question snip

int a[]={1,2,3,4}
int *p = a++;
cout<<*p<<endl;

I wanted to know why the above code output will generate an error and which line will generate an error?

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
Shubham
  • 25
  • 8
  • 2
    `a` isn't a pointer. It's automatically converted to a pointer in some cases, including this case, but the resulting pointer is a temporary (it's not stored anywhere before the conversion, and is computed on the fly) (it's an rvalue, more specifically prvalue). You can't modify temporaries like this (at least of non-class types), it's the same thing as trying to do `42++`. – HolyBlackCat Jan 05 '23 at 18:31
  • 1
    "which line will generate an error?" - You have the code. Compile it! It will give you the error message with a line number. From the error you might also be able to figure out "why the above code output will generate an error " – Thomas Weller Jan 05 '23 at 18:34
  • ok, Thank You I am learning the pointers topic. I used to code in Java which doesn't have this concept in the fore-end. I just wanted to understand how it shows an error. As per my understanding, I was expecting 2 as the answer. – Shubham Jan 05 '23 at 18:46
  • Terminology note: [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/q/3601602/4581301) – user4581301 Jan 05 '23 at 19:22

0 Answers0