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 <BC@sierradesign.com> wrote:
: 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

This is not ANSI C (C89) no. And I do not even think C99 is allowing
this, but did not check.

: not under the QNX Watcom compiler.

You could do
int myArray [10] = {47, 47, 47, 47, 47, 47, 47, 47, 47, 47};

:wink:, looks cumbersome he !.

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

This is a gcc extension, see the gcc manual for other “goodies”.
I believe it was there in gcc-2.8.x and going further then gcc-2.8.x
is not worth the effort. Although the Linux kernel could only
be compile with 2.7.2.x only for along time, and constructs like
this was part of it(with other gcc extensions). So 2.7.2.x look
like a safe bet too.


au revoir, alain

Aussi haut que l’on soit assis, on n’est toujours assis que sur son cul !!!

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

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/

Don’t worry but Watcom 10.6 won’t like it (we tried).
we prefer s.th like that:

int array[10] = {0};

main ()
{
int ii;
if ( !array[0] ) for (ii=0;ii<10;ii++) array[ii] = 47;
}

the finest aspect of such simple code is we could (could we ?..) understand
it if finding our bug some time in the future.

regards
m.Koehler

Bill at Sierra Design <BC@SierraDesign.com> schrieb in im Newsbeitrag:
8ooic3$pkl$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?