What the hell is that ?

Hi All,

A co-worker of mine was just showing me a C construct that neither of us
have ever seen before.

Here it is:

int myArray [10] = { [0 … 9] = 47 };

This is a slightly simplified version of what we actually saw. What we saw
was initializing an array of structures of interrupt handlers vectors. It
came from a Linux system. Now it’s fairly obvious what the intent is. And
we’re all wanted to do something like this before. But I didn’t think that
this was legal C. For the record, it does compile under the Linux gcc but
not under the QNX Watcom compiler.

Does anyone know if this is a new language feature or just some gcc
extension to the language and when it was introduced?

Bill at Sierra Design wrote in message <8ooib5$pkb$1@inn.qnx.com>…

Hi All,

A co-worker of mine was just showing me a C construct that neither of us
have ever seen before.

Here it is:

int myArray [10] = { [0 … 9] = 47 };

This is a slightly simplified version of what we actually saw. What we saw
was initializing an array of structures of interrupt handlers vectors. It
came from a Linux system. Now it’s fairly obvious what the intent is. And
we’re all wanted to do something like this before. But I didn’t think that
this was legal C. For the record, it does compile under the Linux gcc but
not under the QNX Watcom compiler.

Does anyone know if this is a new language feature or just some gcc
extension to the language and when it was introduced?

There is a more portable way to do this, like this:

#include <j2k/Fred/Util/RepeatWC.hpp>

// Look at this address:
http://j2k.sourceforge.net/src/j2k/Fred/Util/RepeatWC.hpp
// #define REPEAT_WC_10(x) x, x, x, x, x, x, x, x, x, x

// I have a script that generated it up to 160 and then
256,512,1024,2048,4096,8192
// if you need more just tell me and I can generate it up to 1,048,576 if
you want =)

int myArray[10] = { REPEAT_WC_10( 47 ) };


// or even better

#define sz 10

int myArray[ sz ] = { REPEAT_WC( sz, 47 ) };

// Means: Repeat With Comma

Sincerly yours,

Fred.

Standard J2K Library for Embedded C++
http://j2k.sourceforge.net/

There is a more portable way to do this, like this:

#include <j2k/Fred/Util/RepeatWC.hpp

// Look at this address:
http://j2k.sourceforge.net/src/j2k/Fred/Util/RepeatWC.hpp
// #define REPEAT_WC_10(x) x, x, x, x, x, x, x, x, x, x

// I have a script that generated it up to 160 and then
256,512,1024,2048,4096,8192
// if you need more just tell me and I can generate it up to 1,048,576 if
you want =)

int myArray[10] = { REPEAT_WC_10( 47 ) };


// or even better

#define sz 10

int myArray[ sz ] = { REPEAT_WC( sz, 47 ) };

// Means: Repeat With Comma

#include <iostream.h>
#include <j2k/Fred/Util/RepeatWC.hpp>

/*
There is even better reason to do that try to do this by hand:
50 times the sequence “11, 23, 45, 32, 16, 98, 32, 55”
512 times the sequence “1, 0”
256 times the sequence:
“Fred”, “Georges”, “Alex”, “Nick”, “Pete”, “Igor”, “Mario”, “Randy”

I don’t suggest more than 512 for space and compile time issues.
512 makes your precompiler rushing to its knees… =)
*/


// Those are requires else the compiler will complain
// since commas are separators and the macro
// needs one.

#define DEF1 11, 23, 45, 32, 16, 98, 32, 55

#define DEF2 1, 0

#define DEF3 “Fred”, “Guy”, “Daniel”, “Bill”,
“Pete”, “Igor”, “Mario”, “Randy”

int main() {

// You must call the correct macro and not REPEAT_WC( 50, DEF1 )
// else the precompiler gets totally mix up ! =)

int seq1[ 50 * 8 ] = { REPEAT_WC_50( DEF1 ) };
int seq2[ 512 * 2 ] = { REPEAT_WC_512( DEF2 ) };

const char* seq3[ 256 * 8 ] = { REPEAT_WC_256( DEF3 ) };

// Quite handy tough.

int i = 0;

for( i = 0; i < (508); i++ ) {
cout << seq1 _<< ’ ';
}

cin >> i; // wait for user

for( i = 0; i < (512
2); i++ ) {
cout << seq2 << ’ ';
}

cin >> i; // wait for user

for( i = 0; i < (256*8); i++ ) {
cout << seq3 << ’ ';
}

return 0;
}

Sincerly yours,

Fred.

Standard J2K Library for Embedded C++
http://j2k.sourceforge.net/_

“Bill at Sierra Design” <BC@SierraDesign.com> wrote in message
news:8ooib5$pkb$1@inn.qnx.com

Hi All,

A co-worker of mine was just showing me a C construct that neither of us
have ever seen before.

Here it is:

int myArray [10] = { [0 … 9] = 47 };

This is a slightly simplified version of what we actually saw. What we
saw
was initializing an array of structures of interrupt handlers vectors. It
came from a Linux system. Now it’s fairly obvious what the intent is.
And
we’re all wanted to do something like this before. But I didn’t think
that
this was legal C. For the record, it does compile under the Linux gcc but
not under the QNX Watcom compiler.

Does anyone know if this is a new language feature or just some gcc
extension to the language and when it was introduced?

I dont know when it was intorduced… but that is a gcc extension. you can
also say things like

int myArray[10] = { [5] 7 };

to init item 5 to 7. If you look in the gcc doco under C Extensions its all
documented(that one is under Label Elements… I just checked :slight_smile:. My
favourite extension on this vein has always been the case ranges. ie

int a;

switch (a)
{
case 1 … 5:
{
// do stuff
}
}

I always liked that syntax in Pascal et al

Cheers,
Ciaran