C++ 指针运算

标签 c++ pointers

给定代码:

int arr[] = {11,22,33,44,55}
for(int i = 0; i <5 ; i++)
    cout << *(arr+i) << " ";

*(arr+i)arr[i]的效果一样吗?

最佳答案

是的。事实上,下标运算符 E1[E2] 被定义为等同于 *((E1)+(E2)):

A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions shall have the type “pointer to T” and the other shall have unscoped enumeration or integral type. The result is an lvalue of type “T.” The type “T” shall be a completely-defined object type. The expression E1[E2] is identical (by definition) to *((E1)+(E2)).

关于C++ 指针运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16223375/

相关文章:

c - 如何在函数内初始化 Struct 指针数组?

c - 将指针传递给具有 volatile 成员的结构作为函数参数

pointers - 循环队列大小

c++ - 派生公共(public)静态 std::list

c++ - 检测错误源 "glibc detected memory corruption"

php - PHP 变量是否应该在用作 OUT 函数参数之前进行初始化?

c++ - 将内存分配给派生类的指针

c++ - 在 Qt 中单击 Dialog 中的 pushButton 时使 MainWindow 可见

c++ - 从“const char [2]”到非标量类型“Persona”的转换请求

c++ - 为什么在 C++20 中删除了许多标准库类型的运算符!=?