[英]How to implement a 2-dimensional array of struct in C
I'm currently trying to understand how to implement a 2-dimensional array of struct in C. My code is crashing all the time and I'm really about to let it end like all my approaches getting firm to C: garbage. 我目前正在尝试理解如何在C中实现结构的二维数组。我的代码一直在崩溃,我真的要让它像我所有的方法一样坚定到C:垃圾。 This is what I got:
这就是我得到的:
typedef struct {
int i;
} test;
test* t[20][20];
*t = (test*) malloc(sizeof(test) * 20 * 20);
My glorious error: 我的光荣错误:
error: incompatible types when assigning to type 'struct test *[20]' from type 'struct test *'
错误:从类型'struct test *'分配类型'struct test * [20]'时出现不兼容的类型
Do I have to allocate the memory seperately for every 2nd dimension? 我是否必须为每个第二维单独分配内存? I'm getting nuts.
我疯了。 It should be so simple.
应该这么简单。 One day I will build a time-machine and magnetize some c-compiler-floppies...
有一天,我将构建一个时间机器并磁化一些c-compiler-floppies ......
This should be enough: 这应该足够了:
typedef struct {
int i;
} test;
test t[20][20];
That will declare a 2-dimensional array of test
of size 20 x 20. There's no need to use malloc. 这将声明一个大小为20 x 20的二维
test
数组。不需要使用malloc。
If you want to dynamically allocate your array you can do this: 如果要动态分配数组,可以执行以下操作:
// in a function of course
test **t = (test **)malloc(20 * sizeof(test *));
for (i = 0; i < 20; ++i)
t[i] = (test *)malloc(20 * sizeof(test));
test **t;
t = (test **)malloc(sizeof(test *) * 20);
for (i = 0; i < 20; i++) {
t[i] = (test *)malloc(sizeof(test) * 20);
}
Other answers show how to fix it but they don't explain why. 其他答案显示如何解决它,但他们没有解释原因。 As the compiler hinted, the type of
t
in your original example is actually test *[20]
which is why your cast to test *
wasn't enough. 正如编译器暗示的那样,原始示例中的
t
类型实际上是test *[20]
,这就是为什么你的演员test *
不够。
In C, the name of an array T of dimension N is actually of type *T[dim0][dim1]...[dimN-1]
. 在C中,维数N的数组T的名称实际上是
*T[dim0][dim1]...[dimN-1]
。 Fun. 乐趣。
From my observation, you may not know exactly what you want and confuse on the struct and pointer arithmetic. 根据我的观察,你可能不知道你想要什么,并混淆结构和指针算术。 Please go through the following 2 possibilities.
请仔细阅读以下两种可能性。
1) A two dimensional array with each element has a pointer to test
. 1)二维数组,每个元素都有一个指向
test
的指针。 In this case the memory of all the pointers to test
s are already statically allocated . 在这种情况下,所有指向
test
的指针的内存已经静态分配 。 But, the memory of the real test
s are not ready. 但是, 真实
test
的记忆尚未准备就绪。 In this case you must fill in the test [i][j]
one by one. 在这种情况下,您必须逐个填写
test [i][j]
。
Each of the test
is discrete in the memory and you have the advantage of create or destroy them individually dynamically. 每个
test
在内存中都是离散的,您可以动态地单独创建或销毁它们。
typedef struct {
int i;
} test;
test* t[20][20];
/* or instead of statically allocated the memory of all the pointers to tests
you can do the following to dynamically allocate the memory
test ***t;
t = (test***)malloc(sizeof(test *) * 20 * 20);
*/
for (int i=0; i < 20; i++){
for (int j=0; j < 20; j++){
t[i][j] = malloc(sizeof(test));
}
}
2) A two dimensional array with each element is a test
. 2)每个元素的二维数组是一个
test
。 In this case the memory of all the test
s are already allocated . 在这种情况下,已经分配 了所有
test
的内存 。 Also, the memory of the real test
s are ready to use without extra preparation. 此外, 真实
test
的内存随时可用,无需额外准备。
All of the test
s are continuous in the memory as a big block and is always there. 所有
test
都在内存中作为一个大块连续存在,并且始终存在。 This mean that you may waste a chunk of memory if you only need all test
s at certain peak time and most of the time you only use a few of them. 这意味着如果您在某个高峰时间只需要所有
test
,并且大多数时间只使用其中一些test
,则可能会浪费大量内存。
typedef struct {
int i;
} test;
test t[20][20];
/* or instead of statically allocated the memory of all tests
you can do the following to dynamically allocate the memory
test **t;
t = (test**)malloc(sizeof(test) * 20 * 20);
*/
Also, as long as your inner dimension size is constant, you can allocate a variable number of counts of that inner dimension 此外,只要您的内部维度大小不变,您就可以分配该内部维度的可变数量的计数
int n = ...;
test (*t)[20] = malloc(sizeof (*t) * n);
t[0 .. (n-1)][0 .. 19] = ...;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.