c - 数组 : accessing array elements by inverting the index variable with the array itself yields the same result

标签 c

<分区>

所以代码看起来像这样(你可以访问这个 fiddle here (Click me):

#include <stdio.h>

int main()
{
    int a[5] = { 6, 2, 7, 3, 5 };

    for (int i = 0; i < 5; i++){
        printf("%d ", i[a]);
    }

    printf("\n");

    for (int i = 0; i < 5; i++){
        printf("%d ", a[i]);
    }

    return 0;
}

这是输出:

6 2 7 3 5
6 2 7 3 5

显然,他在对数组进行索引时犯了一个错误,将索引变量与数组本身进行了倒置。他发现它仍然打印相同的值。 这可能是一个愚蠢的问题,但同样,我对这些类型的案例了解不多,这让我很想知道为什么会发生这里发生的事情。

谢谢

最佳答案

x[y] 等价于 *(x + y) 等价于 *(y + x) 等价到 y[x]

关于c - 数组 : accessing array elements by inverting the index variable with the array itself yields the same result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54628615/

相关文章:

c - 使用以下 C 代码,整数变量是否有可能达到值 -1?

c - 当程序在fclose()之前结束时会出现什么问题

c - 搜索/打印数组中的特定元素

c++ - 通过串行向微 Controller 设置值

使用curl将数据从URL复制到文件中并同时访问该文件

C - 二维矩阵,以两个对称的上下三角形的形状相加(几乎像X)

c - 理解 C 指针

c - LPVOID 和 void* 之间的区别

c - 写数组到串口c

c - gmp 值转换为 char*