Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring int array inside struct

Tags:

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
};
like image 942
Bob Avatar asked Jun 22 '13 12:06

Bob


People also ask

Can you place an array inside a structure C++?

Yes, that is completely doable.

Is it possible to initialize an array of structure explain?

It's called designated initializer which is introduced in C99. It's used to initialize struct or arrays, in this example, struct . Show activity on this post. It's a designated initializer, introduced with the C99 standard; it allows you to initialize specific members of a struct or union object by name.

Can I use struct as array?

An array of structres in C can be defined as the collection of multiple structures variables where each variable contains information about different entities. The array of structures in C are used to store information about multiple entities of different data types.


2 Answers

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.

like image 160
Bertie92 Avatar answered Oct 13 '22 09:10

Bertie92


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;
like image 26
Yu Hao Avatar answered Oct 13 '22 09:10

Yu Hao