C 中的二维数组初始化
我知道这是一个老栗子,但我想要在我的代码中静态分配一个小的二维数组。我知道执行此操作的方法是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
多维数组不会成为多级指针(我不知道正确的术语)。只有一维会衰减。例如:
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]
becomesint *
;int [20][5]
becomesint (*)[5]
(which is notint **
); 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 ofa[r][c]
, usea[r*C + c]
).最快的方法是将参数
a
声明为int a[][2]
。The quickest way is to declare argument
a
asint a[][2]
.您已接近上一节,但需要稍微不同的声明。生成的 A 可以作为
int **
传递给函数。这样,
print_matrix(A, 3, 2);
输出: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: