C语言 如何在C中实现结构的二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3275381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:55:41  来源:igfitidea点击:

How to implement a 2-dimensional array of struct in C

cstructmultidimensional-array

提问by PenthousePauper

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 编译器软盘......

回答by IVlad

This should be enough:

这应该足够了:

typedef struct {
    int i;
} test;

test t[20][20];

That will declare a 2-dimensional array of testof size 20 x 20. There's no need to use malloc.

这将声明一个test大小为 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));

回答by BobTurbo

test **t;

t = (test **)malloc(sizeof(test *) * 20);
for (i = 0; i < 20; i++) {
   t[i] = (test *)malloc(sizeof(test) * 20);
}

回答by msw

Other answers show how to fix it but they don't explain why. As the compiler hinted, the type of tin 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]. Fun.

在 C 中,维度为 N 的数组 T 的名称实际上是类型*T[dim0][dim1]...[dimN-1]。乐趣。

回答by ttchong

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.

根据我的观察,您可能不确切地知道自己想要什么,并且对结构和指针算法感到困惑。请通过以下 2 种可能性。

1) A two dimensional array with each element has a pointer to test. In this case the memory of all the pointers to testsare already statically allocated. But, the memory of the real testsare not ready. In this case you must fill in the test [i][j]one by one.

1) 一个二维数组,每个元素都有一个指向 的指针test。在这种情况下,所有指向tests的指针内存都已经静态分配了。但是,real tests内存还没有准备好。在这种情况下,您必须test [i][j]一一填写 。

Each of the testis 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. In this case the memory of all the testsare already allocated. Also, the memory of the real testsare ready to use without extra preparation.

2) 每个元素的二维数组是 a test。在这种情况下,所有tests内存都已分配。此外,real tests内存无需额外准备即可使用。

All of the tests 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 tests at certain peak time and most of the time you only use a few of them.

所有的tests 作为一个大块在内存中是连续的,并且一直存在。这意味着如果您只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);
*/ 

回答by Johannes Schaub - litb

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] = ...;