指针问题C++,未正确遍历

pointer issues in C++, not traversing correctly

本文关键字:遍历 问题 C++ 指针      更新时间:2023-10-16
    int main()
{
    const int x = 4;
    int row, col, j;
    int myArray[x][x], *xptr;

    for (row = 0; row < 4; row++){
        for (col = 0; col < 4; col++){
            myArray[row][col] = (row * 4) + col;
        }
    }
    printf("contents of myArray... n");
    /* set xptr to the front of myArray*/
    xptr = &(myArray[0][0]);
    /*Print out the contents of the array. */
    for (row = 0; row < 4; row++) {
        for (col = 0; col < 4; col++) {
            printf("%d ", *(xptr + (row * 4) + col));
        }
    }
    printf("n");
    /* Print out contents again, this time increment the pointer. */
    printf(" Contents of the x array again... n");
    /* Set xptr to the front of the x array. */
    xptr = &(myArray[0][0]);
    /*print out the contents of the array. */

        for (j = 0; j< 16; j++);
        printf("%d ", *xptr);
        xptr++;
    }
    }

我无法使用指针作为打印地址重新打印数组。IE,根据我的教授笔记,我应该得到x 数组的内容...

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

再次显示 x 数组的内容...

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

但相反,我得到了

contents of myArray...
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
contents of the x array again...
0

然后它结束了。我已经尝试过玩所有东西,但我不确定是什么给了我这个错误

谢谢克里彻

    for (j = 0; j< 16; j++);

看到行尾那个额外的分号了吗?

看起来你的牙套也不对,对于第二个循环。