我看到了一个奇怪的程序 here .
int main()
{
int s[]={3,6,9,12,18};
int* p=+s;
}
上面的程序在 GCC 和 Clang 编译器上测试并且在两个编译器上都可以正常工作。
我很想知道,int* p=+s;
是做什么的?
数组s
是否衰减为指针类型?
最佳答案
内置operator+
可以将指针类型作为其操作数,因此将数组 s
传递给它会导致 array-to-pointer conversion然后返回指针int*
。这意味着您可以单独使用 +s
来获取指针。 (对于这种情况,它是多余的;没有 operator+
它也会衰减为指针,然后分配给 p
。)
(强调我的)
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.
关于c++ - "int* p=+s;"有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50598177/