Is this legal in standard C or just a gcc feature? 
Author Message
 Is this legal in standard C or just a gcc feature?

Hi there
In gcc under cygwin I found I could do the following cleverness

##########

typedef struct {
    int i;
    int j;

Quote:
}MyStruct;

int main () {
    int i;

    i = ((MyStruct) { 3, 4 } ).j ;

    printf("i= %d\n", i );

Quote:
}

##########

The program output as expected is

i = 4

However is this strictly legal C programming or just a gcc trick? I know
that Microsoft Visual Studio 6.0 compiler refuses to compile it and so does
the Diab PowerPC compiler from WindRiver.

Brad Phelan



Sun, 03 Apr 2005 20:33:06 GMT  
 Is this legal in standard C or just a gcc feature?

Quote:
> Hi there
> In gcc under cygwin I found I could do the following cleverness

> ##########

> typedef struct {
>     int i;
>     int j;
> }MyStruct;

> int main () {
>     int i;

>     i = ((MyStruct) { 3, 4 } ).j ;

>     printf("i= %d\n", i );

> }

> ##########

> The program output as expected is

> i = 4

> However is this strictly legal C programming or just a gcc trick? I know
> that Microsoft Visual Studio 6.0 compiler refuses to compile it and so does
> the Diab PowerPC compiler from WindRiver.

This feature is known as "compound literal" and has been added
with C99. Compilers conforming to the older version may not
support it or may support it as an extension. You may try gcc
with the -ansi and -pedantic switches to compile the source in
conforming mode (I'm not sure whether you'll also have to apply
-std=C90 with newer gccs).
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond


Sun, 03 Apr 2005 20:55:50 GMT  
 Is this legal in standard C or just a gcc feature?

Quote:


> > In gcc under cygwin I found I could do the following cleverness

> > ##########
> > typedef struct {
> >     int i;
> >     int j;
> > }MyStruct;

> > int main () {
> >     int i;

> >     i = ((MyStruct) { 3, 4 } ).j ;
> >     printf("i= %d\n", i );
> > }
> > ##########

> > The program output as expected is

> > i = 4

> > However is this strictly legal C programming or just a gcc trick?
> > I know that Microsoft Visual Studio 6.0 compiler refuses to
> > compile it and so does the Diab PowerPC compiler from WindRiver.

> This feature is known as "compound literal" and has been added
> with C99. Compilers conforming to the older version may not
> support it or may support it as an extension. You may try gcc
> with the -ansi and -pedantic switches to compile the source in
> conforming mode (I'm not sure whether you'll also have to apply
> -std=C90 with newer gccs).

Ugh.  What an ugly and pointless way to write "i = 4".  If there
was an embedded assignment of the whole struct I could at least
see *some* point to it.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Sun, 03 Apr 2005 23:39:28 GMT  
 Is this legal in standard C or just a gcc feature?
Thanks for this info.

Brad


Quote:

> > Hi there
> > In gcc under cygwin I found I could do the following cleverness

> > ##########

> > typedef struct {
> >     int i;
> >     int j;
> > }MyStruct;

> > int main () {
> >     int i;

> >     i = ((MyStruct) { 3, 4 } ).j ;

> >     printf("i= %d\n", i );

> > }

> > ##########

> > The program output as expected is

> > i = 4

> > However is this strictly legal C programming or just a gcc trick? I know
> > that Microsoft Visual Studio 6.0 compiler refuses to compile it and so
does
> > the Diab PowerPC compiler from WindRiver.

> This feature is known as "compound literal" and has been added
> with C99. Compilers conforming to the older version may not
> support it or may support it as an extension. You may try gcc
> with the -ansi and -pedantic switches to compile the source in
> conforming mode (I'm not sure whether you'll also have to apply
> -std=C90 with newer gccs).
> --

> "LISP  is worth learning for  the profound enlightenment  experience
> you will have when you finally get it; that experience will make you
> a better programmer for the rest of your days."   -- Eric S. Raymond



Mon, 04 Apr 2005 16:36:17 GMT  
 Is this legal in standard C or just a gcc feature?

Quote:

> Hi there
> In gcc under cygwin I found I could do the following cleverness

> ##########

> typedef struct {
>     int i;
>     int j;
> }MyStruct;

> int main () {
>     int i;

>     i = ((MyStruct) { 3, 4 } ).j ;

>     printf("i= %d\n", i );

> }

> ##########

> The program output as expected is

> i = 4

> However is this strictly legal C programming or just a gcc trick? I know
> that Microsoft Visual Studio 6.0 compiler refuses to compile it and so does
> the Diab PowerPC compiler from WindRiver.

Many compilers allow several choices of standards.

GCC, for example, with flag your code with
[for -std=iso9899:1990 or -std=9899:199409 or -std=gnu89]
a.c: In function `main':
a.c:9: warning: ISO C89 forbids compound literals
a.c:10: warning: implicit declaration of function `printf'
a.c:11: warning: control reaches end of non-void function

but only with
[for  -std=iso9899:1999 or -std=gnu99]
a.c: In function `main':
a.c:10: warning: implicit declaration of function `printf'



Mon, 04 Apr 2005 16:36:48 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Are nested functions legal in ANSI/ISO Standard C

2. A badly missed feature in gcc

3. gcc-cpp chokes: bug or feature?

4. Newbie: separate big .cs file into small .cs files

5. bug, or feature, in standard math lib???

6. New C standard features

7. Which new features would we like to have in the next C standard

8. ANSI standard C -- who's right Borland or gcc

9. Standard Library of gcc compiler?

10. Preprocessor question (is gcc standard)?

11. I am new to programming and am lost

12. gcc g++: File.h for gcc - did you ever seen

 

 
Powered by phpBB® Forum Software