Declaring int array inside struct

谁说我不能喝 提交于 2020-11-30 04:59:56

问题


In C, I have defined the struct seen below, and would like to initialize it inline. Neither the fields inside the struct, nor the array foos will change after initialization. The code in the first block works fine.

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 tmp field. In fact, it will just clutter my code (this example is somewhat simplified). So, instead I'd like to declare the values of some_array inside the declaration for foos. I cannot get the right syntax, though. Maybe the field some_array should be defined differently?

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
};

回答1:


int *some_array;

Here, some_array is actually a pointer, not an array. You can define it like this:

struct Foo {
  int bar;
  int some_array[3];
};

One more thing, the whole point of typedef struct Foo Foo; is to use Foo instead of struct Foo. And you can use typedef like this:

typedef struct Foo {
  int bar;
  int some_array[3];
} Foo;



回答2:


First, there are 2 ways:

  • 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 struct Foo {
   ...
} Foo;

it means you replace the "struct Foo" words with this: "Foo". So the syntax will be this:

Foo foo;   //instead of "struct Foo foo;

Cheers.




回答3:


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.

struct Foo foos[] = {
  {123, tmp},   // works
  {222,  11,22,33 },  // would compile perfectly.
}


来源:https://stackoverflow.com/questions/17250480/declaring-int-array-inside-struct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!