c++ - "int* p=+s;"有什么作用?

标签 c++ arrays pointers unary-operator

我看到了一个奇怪的程序 here .

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

上面的程序在 GCCClang 编译器上测试并且在两个编译器上都可以正常工作。

我很想知道,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/

相关文章:

c++ - 返回 shared_ptr<> 的成员函数的 const 正确性

c++ - 在 C++ 中使用 switch 评估字符串

Java:数组排序

c - 结构中的二维动态数组

c - 在 C 中打印矩阵时获取垃圾值

c++指针问题 - 通过方法更新指针

c++ - 如何打印仅小于另一个 double 值的 double 值?

javascript - 为什么我的代码没有生成 "-"的数组,并在所选列处生成 "X"?

c++ - 将成员函数的本地存储分配移动到其类是否可以获得性能?

c - 指向 struct 的双指针中的第一个元素是 jiberrish