C语言 在结构中声明 int 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17250480/
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
Declaring int array inside struct
提问by Bob
In C, I have defined the structseen below, and would like to initialize it inline. Neither the fields inside the struct, nor the array fooswill change after initialization. The code in the first block works fine.
在 C 中,我定义了struct下面的内容,并希望对其进行内联初始化。结构体内部的字段和数组foos在初始化后都不会改变。第一个块中的代码工作正常。
struct Foo {
int bar;
int *some_array;
};
typedef struct Foo Foo;
int tmp[] = {11, 22, 33};
struct Foo foos[] = { {123, tmp} };
However, I don't really need the tmpfield. In fact, it will just clutter my code (this example is somewhat simplified). So, instead I'd like to declare the values of some_arrayinside the declaration for foos. I cannot get the right syntax, though. Maybe the field some_arrayshould be defined differently?
但是,我真的不需要这个tmp领域。事实上,它只会使我的代码变得混乱(这个例子有点简化)。所以,相反,我想some_array在foos. 但是,我无法获得正确的语法。也许该领域some_array应该有不同的定义?
int tmp[] = {11, 22, 33};
struct Foo foos[] = {
{123, tmp}, // works
{222, {11, 22, 33}}, // doesn't compile
{222, new int[]{11, 22, 33}}, // doesn't compile
{222, (int*){11, 22, 33}}, // doesn't compile
{222, (int[]){11, 22, 33}}, // compiles, wrong values in array
};
采纳答案by Yu Hao
int *some_array;
Here, some_arrayis actually a pointer, not an array. You can define it like this:
这里,some_array实际上是一个指针,而不是一个数组。你可以这样定义它:
struct Foo {
int bar;
int some_array[3];
};
One more thing, the whole point of typedef struct Foo Foo;is to use Fooinstead of struct Foo. And you can use typedef like this:
还有一件事,重点typedef struct Foo Foo;是使用Foo而不是 struct Foo. 你可以像这样使用 typedef:
typedef struct Foo {
int bar;
int some_array[3];
} Foo;
回答by Bertie92
First, there are 2 ways:
首先,有2种方式:
- You know that array's size
- You don't know that size.
- 你知道数组的大小
- 你不知道那个尺寸。
In the first case it is a static programming problem, and it is not complicated:
第一种情况是静态编程问题,并不复杂:
#define Array_Size 3
struct Foo {
int bar;
int some_array[Array_Size];
};
You can use this syntax to fill the array:
您可以使用以下语法来填充数组:
struct Foo foo;
foo.some_array[0] = 12;
foo.some_array[1] = 23;
foo.some_array[2] = 46;
When you dont know the array's size, it is a dynamic programming issue. You have to ask the size.
当您不知道数组的大小时,这是一个动态规划问题。你得问尺寸。
struct Foo {
int bar;
int array_size;
int* some_array;
};
struct Foo foo;
printf("What's the array's size? ");
scanf("%d", &foo.array_size);
//then you have to allocate memory for that, using <stdlib.h>
foo.some_array = (int*)malloc(sizeof(int) * foo.array_size);
//now you can fill the array with the same syntax as before.
//when you no longer need to use the array you have to free the
//allocated memory block.
free( foo.some_array );
foo.some_array = 0; //optional
Second, typedef is useful, so the when you write this:
其次,typedef 很有用,所以当你写这个的时候:
typedef struct Foo {
...
} Foo;
it means you replace the "struct Foo" words with this: "Foo". So the syntax will be this:
这意味着您将“struct Foo”替换为:“Foo”。所以语法是这样的:
Foo foo; //instead of "struct Foo foo;
Cheers.
干杯。
回答by smitha
My answer is for the following code section:-
我的答案是针对以下代码部分:-
int tmp[] = {11, 22, 33};
struct Foo foos[] = {
{123, tmp}, // works
{222, {11, 22, 33}}, // doesn't compile
{222, new int[]{11, 22, 33}}, // doesn't compile
{222, (int*){11, 22, 33}}, // doesn't compile
{222, (int[]){11, 22, 33}}, // compiles, wrong values in array
};
All the above compilation issues is due to it not being compatible with the ANSI standards, the aggregate 'foos' is having subaggregates some of which are bracketed while others are not. So if you remove the internal brackets to represent the array 'tmp' it would compile without fail. For eg.
所有上述编译问题都是由于它与 ANSI 标准不兼容,聚合 'foos' 具有子聚合,其中一些被括起来,而另一些则没有。因此,如果您删除内部括号来表示数组 'tmp',它将顺利编译。例如。
struct Foo foos[] = {
{123, tmp}, // works
{222, 11,22,33 }, // would compile perfectly.
}

