delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/11/03:38:22

Message-ID: <325D582F.FDB@pobox.oleane.com>
Date: Thu, 10 Oct 1996 22:10:23 +0200
From: Francois Charton <deef AT pobox DOT oleane DOT com>
Organization: CCMSA
MIME-Version: 1.0
To: "A.Appleyard" <A DOT APPLEYARD AT fs2 DOT mt DOT umist DOT ac DOT uk>
CC: djgpp AT delorie DOT com
Subject: Re: Labelled array display element funny
References: <7E03B4350B AT fs2 DOT mt DOT umist DOT ac DOT uk>

A.Appleyard wrote:
> 
>   This program:-
> 
> 1    #include<stdio.h>
> 2    typedef struct{char *codeno; char*name;} craft;
> 3    craft dsub[128]={
> 4         [1] {"CH79","Aphanistor"},
> 5        [55] {"DS1 ","Quackers"},
> 6        [56] {"DS2 ","Donald"},
> 7        [23] {"BA32","Big Jim"},
> 8        [35] {"FA65","Trelawney"}};
> 9    main(){}
> 
>   produced these errors:-
> t$.cc:5: parse error before `['
> t$.cc:5: warning: aggregate has a partly bracketed initializer
> t$.cc:6: parse error before `['
> 
>   What have I done wrong here? Or can't I use array element labels when the
> elements are subarray displays?

Yes you can, your program is right, but its name is wrong...

>From the gcc info file (section C extensions, subsection Labelled 
elements):

>Labeled Elements in Initializers
>================================
>
>   Standard C requires the elements of an initializer to appear in a
>fixed order, the same as the order of the elements in the array or
>structure being initialized.
>
>   In GNU C you can give the elements in any order, specifying the array
>indices or structure field names they apply to.  This extension is not
>implemented in GNU C++.
>

Your program syntax is allright in GNU C, but not in C++... But using the 
file name "t$.cc" (with extension .cc) lures gcc into thinking you are 
compiling a C++ program (see the FAQ section 8.4), and there, what you 
wrote is forbidden.

To fix this, either rename t$.cc as t$.c (mind the lower case for the 
extension .c, if you call it .C, gcc will think it is C++, and ... 
kaboom), or use the gcc "-x c" option (which means your file, whatever 
its name, is a C program).

Regards,
Francois

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019