Accessing an entire row of a multidimensional array in C++

前端 未结 3 794
鱼传尺愫
鱼传尺愫 2021-01-12 13:47

How would one access an entire row of a multidimensional array? For example:

int logic[4][9] = {
    {0,1,8,8,8,8,8,1,1},
    {1,0,1,1,8,8,8,1,1},
    {8,1,0         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-12 14:20

    A direct assignment won't work. C++ does not allow that. At best you'll be able to assign them to point to the same data - int *temp = logic[2]. You'll need a for loop or something like the below.

    I believe this would work:

    int temp[9];
    memcpy(temp, logic[2], sizeof(temp));
    

    But I'd generally suggest using std::vector or std::array instead.

提交回复
热议问题