1

How to initialize an array in c with incrementing values at compile time?

For example I want to initialize an array of 5000 integers - and I want to initialize it like a[] = {1,2,....4999} I dont want to write 5000 integers while initializing the array at compile time.

Paul R
  • 195,989
  • 32
  • 353
  • 519

7 Answers7

4

You could do it with a bunch of macros - its evil and it's ugly and I can't see why you'd want to, but here you go:

#define V10(x) (x), (x)+1, (x)+2, (x)+3, (x)+4, (x)+5, (x)+6, (x)+7, (x)+8, (x)+9

#define V100(x) V10(x), V10(x+10), V10(x+20), V10(x+30), V10(x+40), \
                V10(x+50), V10(x+60), V10(x+70), V10(x+80), V10(x+90)

#define V1000(x) V100(x), V100(x+100), V100(x+200), V100(x+300), V100(x+400), \
                 V100(x+500), V100(x+600), V100(x+700), V100(x+800), V100(x+900)

const int a[5000] = { V1000(1), \
                      V1000(1001), \
                      V1000(2001), \
                      V1000(3001), \
                      V1000(4001) };
Paul R
  • 195,989
  • 32
  • 353
  • 519
  • @Neil: quite - I was giving the OP the benefit of the doubt though - maybe there is a genuine requirement (unlikely I know), e.g. it's some embedded thing that has a real-time constraint on startup time... – Paul R Sep 17 '13 at 09:48
  • @PaulR - Exactly. The system reboots quite a lot and it needs to be up as soon as possible. Apart from this, why do something at run time, which can easily be done at compile time. – Sumit Nathany Sep 17 '13 at 09:56
  • @Sumit: well if you can avoid a copy (e.g. if it's a const array and it is loaded directly from a const segment) then there might be a valid case, but otherwise you're just trading an initialisation loop for a copy loop and performance will not be very different. – Paul R Sep 17 '13 at 09:59
3

For C, your best bet might be to use an external script to generate the C code, like:

$ echo "unsigned int myarray[] = { $(seq -s ", " 0 9) };"
unsigned int myarray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

You can pipe that into a dedicated source file and include that in your code.

(In C++ you have better options thanks to template magic.)

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
2

You can't initialize the array directly with the incrementing values in C. You will have to either initialize it by specifying all the values or using a loop at runtime.

HAL
  • 3,663
  • 3
  • 17
  • 28
0

Initializing the array directly with the incrementing values in C is not possible, although it may be possible using linker scripts as mentioned by others. Best is to use a for loop... for(i = 0, i < 5000, i++) a[i] = i + 1;

Trilok M
  • 601
  • 7
  • 18
0

For smaller arrays, you could do like this:

#include <stdio.h>

#define OFFSET (9 - 1)  // start at line 9, -1 for 1 indexing

int main()
{
  int array[] = 
  {
    __LINE__ - OFFSET,
    __LINE__ - OFFSET,
    __LINE__ - OFFSET,
    __LINE__ - OFFSET,
    __LINE__ - OFFSET
  };

  for(int i=0; i<sizeof(array)/sizeof(*array); i++)
  {
    printf("%d ", array[i]);
  }
}

But of course with 5000 integers, this method will be quite tedious. The best option is to generate the C source code from an external script.

Lundin
  • 155,020
  • 33
  • 213
  • 341
  • (For example, every beginner C programmer should be capable of writing a program that prints `{1, 2, 3.... 5000}` to a text file.) – Lundin Sep 17 '13 at 09:59
0

Use an external assembly file to initialize the data and compile it with your C code. You may need to specify the .data segment directive.

TwoCode
  • 125
  • 7
-1

Why not do something like this:

int size = 5000;
int arr[size];
int i = 1;
for(i=1; i <= size; i++)
{
    arr[i - 1] = i; // Initializing each element seperately
}
Armand
  • 8,929
  • 8
  • 35
  • 73