36

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)

13
  • there's wmemset() for "wide" char arrays Commented Nov 20, 2012 at 16:10
  • wouldn't other libs require >1 line? #include libother Commented Nov 20, 2012 at 16:12
  • @MarcB - Not bad... two minor issues, first I understand wchar_t is compiler-specific and can be as small as 8 bits so that could be single byte. Second, I was hoping for something that could work on different types. But not a bad suggestion. Thanks. Commented Nov 20, 2012 at 16:18
  • Mac OS X has memset_pattern4(), memset_pattern8() and memset_pattern16() since version 10.5. Commented Nov 20, 2012 at 16:19
  • 1
    @mcalex - Touché. Ok, how about "1 liner per setting", we won't count #includes or more options to gcc Commented Nov 20, 2012 at 16:19

7 Answers 7

41

This is a GCC extension:

int a[100] = {[0 ... 99] = 1};
Sign up to request clarification or add additional context in comments.

8 Comments

That is awesome, I was not aware of the syntax. +1
Note to anyone else using this, there's white space between the digits and the ... which is important
@Mike check the link, you can also initialize multiple ranges and single elements.
Correct me if I'm wrong, but designated initializers in the C language are: typedef struct { int x; int y} type; then type t = { .x=1, .y=2 }; I have never seen the .. notation nor heard it referred to as designated initializer. I believe this is some GCC extension beyond standard C designated initializers.
Can this be extended to multi-dimensional arrays?
|
26
for (count = 0; count < 30; count++) arr[count] = 1;

One line. :)

1 Comment

One line. :) very true... the letter of the law was followed with this solution
23

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
};

5 Comments

Very neat solution, readable and extendable and compile-time!
@Lundin: This is by far the best, smart and readable. Thanks!
Could you generalize this a little to functions taking the number to be repeated? i.e. #define ONE(n) 1, then #define FIVE(n) ONE(n), ONE(n), ONE(n), ONE(n), ONE(n), etc, so you don't need a separate set of macros for every number?
Actually, with a little clever trickery you could probably even generalize it to a function #define REPEAT(symbol, count), though I'm too tired to figure out how that would work at the moment :)
@NicHartley Indeed, you can use this method to create more elegant macros. #define FIVE(n) does indeed look prettier than FIVE1.
21

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++);

2 Comments

Alternatively: for(count = 0; count < 30; arr[count++] = 1);
You can also write your whole C program on one single line. If someone could provide a rationale over how that makes sense, I'd be glad to hear it.
12

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);

3 Comments

That's actually a really going point, why I didn't think of including just a macro to take care of it is beyond me...
If you make an icky macro like this, why not write one to actually initialize the array, rather than setting it in runtime like this? Suppose "arr" has static storage duration and you want to take advantage of the special initialization rules for such variables (they are initialized before main is called).
@Lundin: If I were aware of any C feature that would allow me to initialize the array as requested, I'd use it. But alas I have no idea how to do it. I see the "icky macro" above as the best thing available in C. If you can suggest anything better, I'm all ears.
8

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;

1 Comment

If you want to be sure that a + 30 will only be calculated once, I would do for (int *p = a, *pEnd = a + 30; p < pEnd; p++) *p = 1;. Your second for loop invokes undefined behaviour, because it calculates a - 1, which is out of bounds. p does not point to the array a, so p >= a is undefined.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.