【问题标题】:Writing to a 2D Array via Pointer Notation通过指针表示法写入二维数组
【发布时间】:2025-12-04 18:55:02
【问题描述】:

我无法理解为什么在下面的 pnArryCpy 中递增指针是不正确的。我想出了如何以不同的方式使用指针表示法复制数组,但我需要了解这有什么问题(例如,(* tgt_s)++; 其中int (*tgt_s)[cs]),以及为什么tgt_s 是左值(例如,tgt_s++是有效的)但*tgt_s 不是(真的)左值。

int main(void)
{

    int arr1[2][4] = { {1, 2, 3, 4}, {6, 7, 8, 9} }; 
    int arr2[2][4];                             

    pnArrCpy(4, arr1, arr2, arr2+2); // copies 2d array using pointer notation 
                                     // - this is where the problem is.
    printarr(2, 4, arr2); // this just prints the array and works fine - not at issue

    putchar('\n');
    return 0;
}

void pnArrCpy(int cs, int (*src)[cs], int (*tgt_s)[cs], int (*tgt_e)[cs])
{
    while (tgt_s < tgt_e)
    {

        **tgt_s=**src;  
        (* tgt_s)++; // older versions of gcc warn "target of assignment not really
                     // an lvalue", latest versions throw an error
        (* src)++;   // but no errors are runtime

    }

    return;
}

// trucated rest of program since it's not relevant, just the function for printing
// the array

在旧的gcc下,程序编译并显示正确的结果,即:

1 2 3 4 
6 7 8 9 

Mac OS 10.8.2
gcc 4.7.2 给了我错误
gcc 4.2.1 只是给我警告

谢谢!!

编辑:我使用可变长度数组的原因:这个函数是另一个程序的一部分,而这个只是我用来排除 pnArrCpy 故障的驱动程序。在实际程序中,数组维度和内容是用户定义的,因此使用VLA。

【问题讨论】:

    标签: c arrays pointers gcc lvalue


    【解决方案1】:

    事情是这样的:

    • int (*tgt_s)[cs] 是一个指向数组的指针。花几秒钟想一想,这有点奇怪
    • *tgt_s 因此是一个数组
    • 数组不是可修改的左值

    最难理解的是您使用 C99 功能传递 cs 然后在参数列表中使用它的方式。

    如果您想了解更多关于 VLA 作为函数参数的信息,请查看this excellent post

    【讨论】:

    • @H2CO3 在参数列表中使用cs 是C99。我链接了 Jens Gustedt 的帖子。
    • 我的意思是编译器如何允许递增数组。
    • @H2CO3 没错。在 WhozCraig 发布的示例中,他增加了 ar,这是一个指针,不是 *ar,这将引发 OP 遇到的相同错误。
    • @H2CO3 你的想法是正确的。正是这个原因导致增量跳跃,在我的示例中,在第一种情况下,40 个字节(10 个整数),在第二种情况下,20 个字节(5 个整数)。 cnicutar 指出,该样本中的指针而不是底层数组是递增的,就像int * p; ++p; 将指针按 sizeof(int) 字节存储一样,因此应使用按其元素大小调整的底层维度的数组指针这样做. IOW,你是对的。
    • @H2CO3 你不是唯一的;该样本证明了 OP 没有做的事情是 okeedokee(durp!)。 =P