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. This is what I got:
我目前正在尝试理解如何在C中实现结构的二维数组。我的代码一直在崩溃,我真的要让它像我所有的方法一样坚定到C:垃圾。这就是我得到的:
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 ......
5 个解决方案
#1
23
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的二维测试数组。没有必要使用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));
#2
6
test **t;
t = (test **)malloc(sizeof(test *) * 20);
for (i = 0; i < 20; i++) {
t[i] = (test *)malloc(sizeof(test) * 20);
}
#3
3
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],这就是为什么你的演员测试*是不够的。
In C, the name of an array T of dimension N is actually of type *T[dim0][dim1]...[dimN-1]
. Fun.
在C中,维数N的数组T的名称实际上是* T [dim0] [dim1] ... [dimN-1]的类型。乐趣。
#4
1
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
. In this case the memory of all the pointers to test
s are already statically allocated. But, the memory of the real test
s are not ready. In this case you must fill in the test [i][j]
one by one.
1)具有每个元素的二维数组具有指向测试的指针。在这种情况下,所有指向测试的指针的内存已经静态分配。但是,实际测试的记忆尚未准备好。在这种情况下,您必须逐个填写测试[i] [j]。
Each of the test
is discrete in the memory and you have the advantage of create or destroy them individually dynamically.
每个测试在内存中都是离散的,您可以动态地单独创建或销毁它们。
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
. In this case the memory of all the test
s are already allocated. Also, the memory of the real test
s are ready to use without extra preparation.
2)每个元素的二维数组是一个测试。在这种情况下,已经分配了所有测试的内存。此外,真实测试的记忆随时可用,无需额外准备。
All of the test
s are continuous in the memory as a big block and is always there. 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.
所有的测试在内存中都是连续的,并且始终存在。这意味着如果您只需要在某个高峰时间进行所有测试,并且大多数时间只使用其中一些测试,则可能会浪费大量内存。
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);
*/
#5
0
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] = ...;
#1
23
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的二维测试数组。没有必要使用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));
#2
6
test **t;
t = (test **)malloc(sizeof(test *) * 20);
for (i = 0; i < 20; i++) {
t[i] = (test *)malloc(sizeof(test) * 20);
}
#3
3
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],这就是为什么你的演员测试*是不够的。
In C, the name of an array T of dimension N is actually of type *T[dim0][dim1]...[dimN-1]
. Fun.
在C中,维数N的数组T的名称实际上是* T [dim0] [dim1] ... [dimN-1]的类型。乐趣。
#4
1
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
. In this case the memory of all the pointers to test
s are already statically allocated. But, the memory of the real test
s are not ready. In this case you must fill in the test [i][j]
one by one.
1)具有每个元素的二维数组具有指向测试的指针。在这种情况下,所有指向测试的指针的内存已经静态分配。但是,实际测试的记忆尚未准备好。在这种情况下,您必须逐个填写测试[i] [j]。
Each of the test
is discrete in the memory and you have the advantage of create or destroy them individually dynamically.
每个测试在内存中都是离散的,您可以动态地单独创建或销毁它们。
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
. In this case the memory of all the test
s are already allocated. Also, the memory of the real test
s are ready to use without extra preparation.
2)每个元素的二维数组是一个测试。在这种情况下,已经分配了所有测试的内存。此外,真实测试的记忆随时可用,无需额外准备。
All of the test
s are continuous in the memory as a big block and is always there. 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.
所有的测试在内存中都是连续的,并且始终存在。这意味着如果您只需要在某个高峰时间进行所有测试,并且大多数时间只使用其中一些测试,则可能会浪费大量内存。
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);
*/
#5
0
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] = ...;