Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize an array of structures with variable index

Tags:

arrays

c

struct

I have structure like below

typedef struct
{
    int a;
    int b;
    int c;
} my_struct;

and in another file I have declared a variable of this my_struct type, like below.

my_struct strct_arr[MAX];

Where MAX is a macro which is a configurable value that is a multiple of 18 (18 or 36 or 54 and so on.. it may go up to 18*n times).

I have to initialize the structure with {0xff,0,0}. So, how to initialize whole array of structure my_struct strct_arr[MAX]; with my initial values without using any kind of loops.

I am expecting the output as below:

my_struct strct_arr[MAX]={
    {0xff,0,0},
    {0xff,0,0},
    {0xff,0,0},
    {0xff,0,0},
    …
};

But without knowing MAX value, how to initialize it?

like image 337
PSR Avatar asked Jun 18 '14 04:06

PSR


People also ask

Can you initialize an array with a variable?

You initialize an array variable by including an array literal in a New clause and specifying the initial values of the array. You can either specify the type or allow it to be inferred from the values in the array literal.

How do you initialize an array of structures?

If you have multiple fields in your struct (for example, an int age ), you can initialize all of them at once using the following: my_data data[] = { [3]. name = "Mike", [2]. age = 40, [1].

How is an array of structures initialized explain using example?

In the following example, we define a struct named Person , which includes 2 char arrays, an int and a bool . Consequently, we declare an array of Person structures and initialize it with curly braced lists as we would the single data type array. Then we output the initialized array elements using the for loop.

Can we initialize arrays in Java using the index number?

The slow way to initialize your array with non-default values is to assign values one by one: int[] intArray = new int[10]; intArray[0] = 22; In this case, you declared an integer array object containing 10 elements, so you can initialize each element using its index value.


2 Answers

There is GCC extension for this. Try this

#define MAX 18
my_struct strct_arr[MAX]={ [0 ... (MAX - 1)] = {0xff,0,0}};

Check https://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Designated-Inits.html

like image 91
Sanket Parmar Avatar answered Oct 10 '22 18:10

Sanket Parmar


Yes, this is possible using the C preprocessor!

#include <stdio.h>
#include <boost/preprocessor/repetition/repeat.hpp>

#define INITS(z, n, t) { 0xFF, 0, 0 },
#define REP(item, n) BOOST_PP_REPEAT(n, INITS, item)

#define MAX 123

typedef struct { int a,b,c; } my_struct;

my_struct ms[] = { REP(, MAX) };

int main()
{
    // Check it worked
    printf("%d\n", (int)(sizeof ms / sizeof *ms));
}

Note: boost is a package of C++ stuff, however the boost/preprocessor just uses the preprocessor features which are common to both languages. If your implementation doesn't allow this #include by default, you can find a copy of repeat.hpp from the boost source code.

Also, BOOST_PP_REPEAT defaults to a max of 256. If your MAX is bigger than this, you can edit repeat.hpp to allow bigger values, it should be obvious what to do from there.

Note: this post describes a system for recursive macro that would not require the same sort of implementation as repeat.hpp uses, but I haven't been able to get it to work.

Credit: this post

like image 25
M.M Avatar answered Oct 10 '22 18:10

M.M