C语言 C中的二维数组初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4409591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:21:52  来源:igfitidea点击:

2D array initialisation in C

cmultidimensional-array

提问by AlastairG

I know this is an old chestnut, but I want a small 2D array statically allocated in my code. I know the way to do this is:

我知道这是一个老栗子,但我想在我的代码中静态分配一个小的二维数组。我知道这样做的方法是:

static int A[3][2] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

That's fine and I can access all the members of it. However I have several problems passing it to a function, e.g.:

没关系,我可以访问它的所有成员。但是我在将它传递给函数时遇到了几个问题,例如:

void print_matrix(int **a, int r, int c)
{
    int x, y;

    for(x = 0; x < r; x++)
    {
        printf("Row %02d = %#x = ", x, a[x]);

        for(y = 0; y < c; y++)
        {
            printf("%s%d", (0 == y) ? "" : ", ", a[x][y]);
        }
        printf("\n");
    }
}

Firstly I can't simply pass Ato the function, I need to cast it to (int **). Since char *is synonymous to char [], I was a little surprised at this. Secondly, it crashes and when I check in the debugger, within the sub-function, a[0]is reported as 1and not a pointer to an array of integers.

首先,我不能简单地传递A给函数,我需要将它转换为 (int **)。既然char *是 的同义词char [],我对此感到有点惊讶。其次,它崩溃了,当我检查调试器时,在子函数中,它a[0]被报告为1而不是指向整数数组的指针。

I know there is compiler/C language arcane magic happening here. But it is all a little confusing. If I try to initialise as:

我知道这里发生了编译器/C 语言的神秘魔法。但这一切都有些令人困惑。如果我尝试初始化为:

static int *A[3] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

I get a ton of warnings. How does this differ to:

我收到了很多警告。这与以下有何不同:

static char *S[3] = { "hello", "there", "stackoverflow" };


Apart from the question of arcane C magic, which somehow I have never learnt despite over a decade of C programming :(, I would like to know how to generate my array so I can successfully pass it as an int **without having to go through all the fag of for loops or copying the statically allocated array to a dynamically allocated one.

除了神秘的 C 魔法的问题,尽管有十多年的 C 编程经验,但我从来没有学过这个问题:(,我想知道如何生成我的数组,这样我就可以成功地将它作为一个传递,int **而不必经历所有的fag for 循环或将静态分配的数组复制到动态分配的数组。

Would the following work?

以下会起作用吗?

int *A0 = { 1, 2 };
int *A1 = { 3, 4 };
int *A2 = { 5, 6 };
int **A = { A0, A1, A2 };

Is there a nicer way than this of doing it?

有没有比这更好的方法呢?

Thanks, all.

谢谢,所有。

P.s. I know that in real life we would read values from a DB or file into dynamically allocated arrays and avoid all this stuff.

Ps 我知道在现实生活中我们会从数据库或文件中读取值到动态分配的数组中并避免所有这些东西。

回答by lijie

A multidimensional array does not become a multi-level pointer (I don't know the proper term). Only one dimension decays. For example:

多维数组不会成为多级指针(我不知道正确的术语)。只有一维衰变。例如:

int [20]becomes int *; int [20][5]becomes int (*)[5](which is not int **); etc.

int [20]变成int *int [20][5]变成int (*)[5](不是int **);等等。

If there is a great desire to use multidimensional arrays (via the [r][c]syntax), then you have to pass the other bounds (which must be constants). If variable bounds are needed, I think the best option is to perform the index conversion manually (i.e. instead of a[r][c], use a[r*C + c]).

如果非常希望使用多维数组(通过[r][c]语法),那么您必须传递其他边界(必须是常量)。如果需要变量边界,我认为最好的选择是手动执行索引转换(即,而不是a[r][c]使用a[r*C + c])。

回答by benmcclelland

int A0[2] = { 1, 2 };
int A1[2] = { 3, 4 };
int A2[2] = { 5, 6 };
int *A[3] = { A0, A1, A2 };

You were close with the last section, but need slightly different declarations. The resulting A can be passed as an int **to a function.

您已经完成了最后一部分,但需要稍微不同的声明。结果 A 可以作为 anint **传递给函数。

With this, print_matrix(A, 3, 2);outputs:

有了这个,print_matrix(A, 3, 2);输出:

Row 00 = 0x5eaeda70 = 1, 2
Row 01 = 0x5eaeda68 = 3, 4
Row 02 = 0x5eaeda60 = 5, 6

回答by Klark

The quickest way is to declare argument aas int a[][2].

最快的方法是将参数声明aint a[][2].

void print_matrix(int[][2] a, int r, int c);

// and call the function like this:
print_matrix(A, 3, 2);