0

Say I have a structure:

struct foo{
    int field_1;
    int field_2;
};

And say have an array:

foo bar[1000];

I understand that you can initialise arrays like this:

foo bar[]={
{ .field_1= 10, field_2 = 20 },
{ .field_1= 30, field_2 = 40 },
};

This is not practical at all when the array has 1000 elements. I need to initialise all elements to a specific integer (at compile time) but it looks like it is not practically possible to do this unless the array is very small. It seems like an essential thing to be able to do but I can't find a solution.

Thanks.

9
  • You can use the loop. Commented Mar 7, 2015 at 8:03
  • I should have mentioned that I want to do this at compile time. Commented Mar 7, 2015 at 8:06
  • At compile time there aren't a lot of options. Put the initialization in a different file and #include it so at least you don't need to look at it. Commented Mar 7, 2015 at 8:08
  • 4
    You can write a program that outputs the C source to initialize the full array. Then include that source into your program. Commented Mar 7, 2015 at 8:08
  • You can put it as global variable or static variable as they are initialised at compile time. Commented Mar 7, 2015 at 8:19

3 Answers 3

0

You can initialize a struct using a for loop like this -

#include<stdio.h>

struct foo{
        int field_1;
        int field_2;
    };

int main(){

    int i;
    struct foo bar[1000];

    for(i=0; i<1000; i++){
        bar[i].field_1 = i;
        bar[i].field_2 = 2*i;
    }

    for(i=0; i<1000; i++){
       printf("bar[%d]:: field_1: %d field_2: %d\n", i, bar[i].field_1, bar[i].field_2);
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

In your solution bar contains integers which is dependent on 'i' or some other variable. But the OP has asked to initialize with specific integers and not some relational values(like i and 2*i) and that too in compile time.
It's just an example. I know all you can take value from scanf() or other source.
Yeah but initialization should take place at compile time. And scanf or other associated methods will work in runtime !!!
0

You can simply create a special "generator" program, which outputs desired C source code for an array with 1000+ initialized elements.

1 Comment

Ye is looks like that is my only option. Thanks.
0

Run a script in your favorite language (e.g., Python), which writes this array into a header file:

fileDesc = open('MyArray.h','wt')
fileDesc.write('#define MY_ARRAY \\\n{ \\\n')
for n in range(0,1000):
    fileDesc.write('\t{.field_1 = ' + '%5d'%(10+20*n) +
                   ' , .field_2 = ' + '%5d'%(20+20*n) + '}, \\\n')
fileDesc.write('}\n')
fileDesc.close()

Then, in the source file, simply include the auto-generated header file and initialize the array:

#include "MyArray.h"
...
foo bar[] = MY_ARRAY;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.