首页 > 解决方案 > &ar[0][0] 和 ar 的区别

问题描述

当我打印ar&ar[0][0]作为指针时,我看到了相同的值。所以我希望它们可以互换,但是,事实并非如此:

在此处输入图像描述

int main(void)
{
    #define rowsCount 4
    #define colsCount 3
    int ar[rowsCount][colsCount] =
    {
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        {9, 10, 11},
    };
    int rowIndex = 1;
    int colIndex = 5;


    printf("TEST: %p %p\n", ar, &ar[0][0]);
    printf("%d\n", ar[rowIndex][colIndex]);
    printf("%d\n", *(&ar[0][0] + rowIndex * colsCount + colIndex));
    printf("%d\n", *(ar + rowIndex * colsCount + colIndex));

    return 0;
}

标签: carrayspointers

解决方案


推荐阅读