C中的二维数组初始化 [英] 2D array initialisation in C

查看:49
本文介绍了C中的二维数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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");
    }
}

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

Firstly I can't simply pass A to 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 1 and not a pointer to an array of integers.

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

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

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" };

<小时>

除了神秘的 C 魔法的问题,尽管我已经学习了十多年的 C 编程,但不知何故我从未学过 :(,我想知道如何生成我的数组,以便我可以成功地将它作为 int 传递** 无需遍历所有 for 循环或将静态分配的数组复制到动态分配的数组.


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.

以下是否可行?

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?

谢谢大家.

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

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.

推荐答案

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

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] 变成 int *;int [20][5] 变成 int (*)[5](不是 int **);等

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

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

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]).

这篇关于C中的二维数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆