Mike
Mike

Reputation: 49463

initializing an array of ints

Does anyone have a way to initialize an array of ints (any multi-byte type is fine really), to a non-zero and non -1 value simply? By which I mean, is there a way to do this in a one liner, without having to do each element individually:

int arr[30] = {1, 1, 1, 1, ...}; // that works, but takes too long to type

int arr[30] = {1}; // nope, that gives 1, 0, 0, 0, ...

int arr[30];
memset(arr, 1, sizeof(arr)); // That doesn't work correctly for arrays with multi-byte
                             //   types such as int

Just FYI, using memset() in this way on static arrays gives:

arr[0] = 0x01010101
arr[1] = 0x01010101
arr[2] = 0x01010101

The other option:

for(count = 0; count < 30; count++)
   arr[count] = 1;    // Yup, that does it, but it's two lines.

Anyone have other ideas? As long as it's C code, no limits on the solution. (other libs are fine)

Upvotes: 36

Views: 13613

Answers (7)

iabdalkader
iabdalkader

Reputation: 17312

This is a GCC extension:

int a[100] = {[0 ... 99] = 1};

Upvotes: 41

Lundin
Lundin

Reputation: 214300

The only sensible way to do this during initialization (rather than runtime) seems to be:

#define ONE1     1
#define FIVE1    ONE1, ONE1, ONE1, ONE1, ONE1
#define TEN1     FIVE1, FIVE1
#define TWENTY1  TEN1, TEN1
#define FIFTY1   TWENTY1, TWENTY1, TEN1
#define HUNDRED1 FIFTY1, FIFTY1

int array [100][4] =
{
  HUNDRED1,
  HUNDRED1,
  HUNDRED1,
  HUNDRED1
};

And next, #define ONE2 2 and so on. You get the idea.

EDIT : The reason why I wrote so many macros was to demonstrate how flexible this solution is. For this particular case you don't need all of them. But with macros like these you can write any kind of initializer list in a quick and flexible way:

{
  FIFTY1, FIFTY2,  // 1,1,1,1... 50 times, then 2,2,2,2... 50 times
  TWENTY3, EIGHTY4 // 3,3,3,3... 20 times, then 4,4,4,4... 80 times
  ... // and so on
};

Upvotes: 22

Bartosz Ciechanowski
Bartosz Ciechanowski

Reputation: 10333

One line with pointers!

for (int *p = a; p < (a + 30); p++) *p = 1;

Or if you're prematurely afraid of performance hit caused by repeatedly calculating (a + 30):

for (int *p = a + 30 - 1; p >= a; p--) *p = 1;

Upvotes: 8

CuriousRabbit
CuriousRabbit

Reputation: 2221

For initialization to a static value, I have generally considered typing it out to be preferred, as in:

int arr[30] = {1, 1, 1, 1, ...}; 

In this case, the compiler can (and usually does) spit out optimized initialization in preamble code.

Sometimes the initialization is more dynamic, as in this example:

int arr[30];
int x = fetchSomeValue();
for(int i=0; i<30; i++) arr[i] = x;

In these cases you have to code it and the general rule is to maximize readability, not minimize typing. This code will be written once and read a multitude of times.

Upvotes: 2

AnT stands with Russia
AnT stands with Russia

Reputation: 320631

In C you typically develop your own "support library" with macros like

#define SET_ALL(a_, n_, v_)\
  do { size_t i, n = (n_); for (i = 0; i < n; ++i) (a_)[i] = (v_); } while(0)

#define SET_ALL_A(a_, v_) SET_ALL(a_, sizeof(a_) / sizeof *(a_), v_)
#define ZERO_ALL(a_, n_) SET_ALL(a_, n_, 0)
#define ZERO_ALL_A(a_) SET_ALL_A(a_, 0)

and then use them in your code as

int arr[30];

SET_ALL_A(arr, 1);

Upvotes: 12

Omkant
Omkant

Reputation: 9214

You said something about 2 lines but you can do it in one line using comma ,operator.

for(count = 0; count < 30 ; arr[count] = 1,count++);

Upvotes: 21

Art
Art

Reputation: 20402

for (count = 0; count < 30; count++) arr[count] = 1;

One line. :)

Upvotes: 26

Related Questions