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.

Neurion
  • 379
  • 6
  • 15
  • You can use the loop. – Karthikeyan.R.S Mar 07 '15 at 08:03
  • I should have mentioned that I want to do this at compile time. – Neurion Mar 07 '15 at 08: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. – Retired Ninja Mar 07 '15 at 08:08
  • 4
    You can write a program that outputs the C source to initialize the full array. Then include that source into your program. – JS1 Mar 07 '15 at 08:08
  • You can put it as global variable or static variable as they are initialised at compile time. – Shreyash S Sarnayak Mar 07 '15 at 08:19
  • @RetiredNinja That seems like the best of a bad situation, thanks. – Neurion Mar 07 '15 at 08:27
  • Write a script in your favorite script language (e.g., Python), that generates this array and writes it into a file. Ideally, you should write it into a header file **as a macro**. This auto-generated header file should contain nothing else besides that macro (and perhaps an `#ifdef` clause). Then, in your source file, simply include the auto-generated header file and set the array to that macro. – barak manos Mar 07 '15 at 08:28
  • It's not a particularly terrible situation. We use a program to convert many of our graphics and fonts to binary data we include at compile time. It isn't pretty to look at the files, but it works. – Retired Ninja Mar 07 '15 at 08:30
  • Compilers are theoretically permitted to optimize your loop to occur at compile-time. gcc doesn't seem to do it though (not with -O3 anyway) – M.M Mar 07 '15 at 09:16

3 Answers3

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;
}
Razib
  • 10,965
  • 11
  • 53
  • 80
  • 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. – Kaustav Ray Mar 07 '15 at 08:31
  • It's just an example. I know all you can take value from scanf() or other source. – Razib Mar 07 '15 at 08:35
  • Yeah but initialization should take place at compile time. And scanf or other associated methods will work in runtime !!! – Kaustav Ray Mar 07 '15 at 08:37
0

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

Matt
  • 13,674
  • 1
  • 18
  • 27
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;
barak manos
  • 29,648
  • 10
  • 62
  • 114