C 中的二维数组初始化

发布于 2024-10-07 08:16:53 字数 1279 浏览 0 评论 0原文

我知道这是一个老栗子,但我想要在我的代码中静态分配一个小的二维数组。我知道执行此操作的方法是:

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

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

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 而不是指向整数数组的指针。

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

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

我会收到大量警告。这与以下有何不同:

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

除了神秘的 C 魔法问题之外,尽管我已经进行了十多年的 C 编程,但我从未学过这个问题:(,我想知道如何生成我的数组,以便我可以成功地将其作为 < 遍历所有 for 循环或将静态分配的数组复制到动态分配的数组,

code>int **无需

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

是否有比这更好的方法,

谢谢, 我知道,在现实

生活中,我们会将数据库或文件中的值读取到动态分配的数组中,并避免所有这些事情。

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两人的回忆 2024-10-14 08:16:53

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

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

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

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.

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

神经暖 2024-10-14 08:16:53

最快的方法是将参数 a 声明为 int a[][2]

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

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

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

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

// and call the function like this:
print_matrix(A, 3, 2);
椒妓 2024-10-14 08:16:53
int A0[2] = { 1, 2 };
int A1[2] = { 3, 4 };
int A2[2] = { 5, 6 };
int *A[3] = { A0, A1, A2 };

您已接近上一节,但需要稍微不同的声明。生成的 A 可以作为 int ** 传递给函数。

这样, print_matrix(A, 3, 2); 输出:

Row 00 = 0x5eaeda70 = 1, 2
Row 01 = 0x5eaeda68 = 3, 4
Row 02 = 0x5eaeda60 = 5, 6
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.

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

Row 00 = 0x5eaeda70 = 1, 2
Row 01 = 0x5eaeda68 = 3, 4
Row 02 = 0x5eaeda60 = 5, 6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文