What does `int* p=+s;` do?

  • A+

I saw weird type of program here.

int main() {     int s[]={3,6,9,12,18};     int* p=+s; } 

Above program tested on GCC and Clang compiler and working fine on both compiler.

I curious to know, what does int* p=+s; do?

here, Is array s decayed to pointer type?


Built-in operator+ could take pointer type as its operand, so when passing the array s to it causes array-to-pointer conversion and then the pointer is returned. That means you might use +s individually to get the pointer. (For this case it's superfluous.)

(emphasis mine)

The built-in unary plus operator returns the value of its operand. The only situation where it is not a no-op is when the operand has integral type or unscoped enumeration type, which is changed by integral promotion, e.g, it converts char to int or if the operand is subject to lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: