从我的观察,你可能不知道你想要什么,并混淆了结构和指针算术。请通过以下2种可能性。
1)具有每个元素的二维数组具有指向test
的指针。 在这种情况下,内存的所有指针test
s已经静态地被分配为。 但是,内存的真实test
s尚未准备好。 在这种情况下,您必须逐个填写test [i][j]
。
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)每个元素的二维数组是test
。 在这种情况下,内存的所有test
s已经分配。 此外,内存的真实test
s准备使用没有额外的准备。
所有的test
都是作为一个大块连续存储在内存中,并始终存在。这意味着如果您在某个高峰时间只需要所有的test
s,并且大部分时间只使用其中的一部分,则可能浪费大量内存。
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);
*/
非常感谢! – Mortezaipo 2017-01-17 21:55:14