c - 关于传递 3 维数组以在 C 中运行的歧义

标签 c arrays pointers multidimensional-array

我在我的程序 (test[2][6][6]) 中定义了 int 的 3 维数组。请看下面给出的程序。我想通过 test[0] 和 test[1] 来显示功能。可以肯定的是,我传递了错误的数组地址函数并得到了无效的结果。我在下面列出了数组值、预期结果和实际结果。任何人都可以通过将正确的数组地址传递给显示函数来帮助我获得预期的结果吗?


程序:

#include <stdio.h>

typedef struct test_t
{
    int i4_x;
} test_t;

void display(test_t (*pas_test)[6][6])
{
    int row_idx = 0, col_idx = 1;
    printf("\nTOP   X: %04d", pas_test[row_idx][col_idx]->i4_x);

    row_idx = 0, col_idx = 5;
    printf("\nLEFT  X: %04d", pas_test[row_idx][col_idx]->i4_x);

    row_idx = 1, col_idx = 0;
    printf("\nRIGHT X: %04d", pas_test[row_idx][col_idx]->i4_x);
    printf("\n");
}

int main()
{
    test_t test[2][6][6];
    int table_idx = 0, row_idx = 0, col_idx = 0, i4_cnt = 0;

    for(table_idx = 0; table_idx < 2; table_idx++)
    {
        for(row_idx = 0; row_idx < 6; row_idx++)
        {
            for(col_idx = 0; col_idx < 6; col_idx++)
            {
                test[table_idx][row_idx][col_idx].i4_x = i4_cnt;
                i4_cnt++;
            }
        }
    }

    /* ARRAY */
    for(table_idx = 0; table_idx < 2; table_idx++)
    {
        for(row_idx = 0; row_idx < 6; row_idx++)
        {
            for(col_idx = 0; col_idx < 6; col_idx++)
            {
                printf("%04d\t",test[table_idx][row_idx][col_idx].i4_x);
            }
            printf("\n");
        }
        printf("\n");
    }
    printf("\n\nTABLE[0]"); display(&test[0]);
    printf("\n\nTABLE[1]"); display(&test[1]);
    return 0;
}

数组值:

TABLE[0]
0000    0001    0002    0003    0004    0005
0006    0007    0008    0009    0010    0011
0012    0013    0014    0015    0016    0017
0018    0019    0020    0021    0022    0023
0024    0025    0026    0027    0028    0029
0030    0031    0032    0033    0034    0035

TABLE[1]
0036    0037    0038    0039    0040    0041
0042    0043    0044    0045    0046    0047
0048    0049    0050    0051    0052    0053
0054    0055    0056    0057    0058    0059
0060    0061    0062    0063    0064    0065
0066    0067    0068    0069    0070    0071

预期结果:

TABLE[0]
TOP   X: 0001
LEFT  X: 0005
RIGHT X: 0006

TABLE[1]
TOP   X: 0037
LEFT  X: 0041
RIGHT X: 0042

实际结果:

TABLE[0]
TOP   X: 0006
LEFT  X: 0030
RIGHT X: 0036

TABLE[1]
TOP   X: 0042
LEFT  X: 0066
RIGHT X: 0072

最佳答案

display 函数的参数 test 是指向 test_t 结构的二维数组的指针。

你正确地传递了那个类型:

printf("\n\nTABLE[0]"); display(&test[0]);
printf("\n\nTABLE[1]"); display(&test[1]);

但是你错误地计算了偏移量。而不是先计算最外层的维度(我们称它为第一维),然后再计算内部的维度(称它们为第二和第三维),如下所示:

      first-v     v-third
test_t test[2][6][6];
               ^-second

printf("\nTOP X: %04d", (*pas_test)[row_idx][col_idx].i4_x);

printf("\nTOP X: %04d", pas_test[0][row_idx][col_idx].i4_x);

您首先使用变量 row_idxcol_idx 计算第一维和第二维,这两个变量分别针对第二维和第三维缩进。

printf("\nTOP   X: %04d", pas_test[row_idx][col_idx]->i4_x);

这等同于

printf("\nTOP   X: %04d", (*( pas_test[row_idx][col_idx])).i4_x);

如您所见,row_idx 用于计算第一个维度,而不是用于计算第二个维度。

相应地更改您的显示功能:

void display(test_t (*pas_test)[6][6])
{
    int row_idx = 0, col_idx = 1;
    printf("\nTOP X: %04d", pas_test[0][row_idx][col_idx].i4_x);

    row_idx = 0, col_idx = 5;
    printf("\nLEFT X: %04d", pas_test[0][row_idx][col_idx].i4_x);

    row_idx = 1, col_idx = 0;
    printf("\nRIGHT X: %04d", pas_test[0][row_idx][col_idx].i4_x);
    printf("\n");
}

或者换一种形式:

    int row_idx = 0, col_idx = 1;
    printf("\nTOP X: %04d", (*pas_test)[row_idx][col_idx].i4_x );

https://ideone.com/UHNOWV

关于c - 关于传递 3 维数组以在 C 中运行的歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29897398/

相关文章:

python - PyArg_ParseTuple 和回调函数指针

c - 在 C 中寻找正确的环形缓冲区实现

javascript - 列出从现在到用户输入的特定数字的所有闰年

javascript - 是否有与 Java 的 deepToString() 等效的 JavaScript?

c - 为什么for循环后没有大括号

c - 如何创建OpenCL命令队列?

c - 将指针传递给对象时类型转换为 (void *)

c++ - C++中的数组指针

ios - Swift 中 SQLite 项目中的 UnsafePointer<Uint8> 问题

c - 将值读入数组失败