Re: Struct Error

Liste des GroupesRevenir à cl c 
Sujet : Re: Struct Error
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.c
Date : 22. Jan 2025, 21:05:44
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250122112439.579@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-01-22, bart <bc@freeuk.com> wrote:
Gcc 14.1 gives me an error compiling this code:
>
   struct vector;
   struct scenet;
>
   struct vector {
       double x;
       double y;
       double z;
   };
>
   struct scenet {
       struct vector center;
       double radius;
       struct scenet (*child)[];
   };
>
The error is:
>
   error: array type has incomplete element type 'struct scenet'
    struct scenet (*child)[];
                   ^~~~~

This looks like a bug to me. There is on reason why an
incomplete array type (a pointer to which /is/ allowed!)
should be constrained to have a complete element.

Especially given that it's going to be complete by the time
it is used (in fact by the time the declaration of struct scenet is
done).

Because the array is incomplete, and we have a pointer to it,
we cannot do arithmetic on it, like child + 1.

All we can do is dereference it, like (*child) or child[0]
to get to the array.

Then when we do child[0][i], then at that time we need the element type
to be complete.

Is there any way to fix this, or is it not possible?

Yes!

Instead of:

  gcc example.c # fails

try:

  g++ example.c # succeeds!

I don't have gcc 14 handy anywhere, but I tried with 7.5 and 11.3,
Same results.

Given that the GCC's C++ front-end accepts it, that bolsters
the case that it should just work in the C front end.

You've remembered to keep your generator's C output C++ compatible,
right? You don't use bleeding edge ISO C features, so you should be
okay.

However, C++ has function overloading and whatnot, so there
is name mangling for some global symbols, like names of functions.

It may be a good idea in the code generator to spit out extern "C"
in the right places, to keep compatibility between modules
generated as C and those generated as C++.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Date Sujet#  Auteur
22 Jan 25 * Struct Error38bart
22 Jan 25 +* Re: Struct Error2Kaz Kylheku
22 Jan 25 i`- Re: Struct Error1Ben Bacarisse
22 Jan 25 +- Re: Struct Error1Richard Harnden
22 Jan 25 +- Re: Struct Error1Lawrence D'Oliveiro
23 Jan 25 +* Re: Struct Error31James Kuyper
23 Jan 25 i+* Re: Struct Error3m137
23 Jan 25 ii+- Re: Struct Error1Tim Rentsch
23 Jan 25 ii`- Re: Struct Error1James Kuyper
23 Jan 25 i+- Re: Struct Error1Tim Rentsch
23 Jan 25 i`* Re: Struct Error26bart
23 Jan 25 i +* Re: Struct Error9BGB
24 Jan 25 i i`* Re: Struct Error8bart
24 Jan 25 i i +- Re: Struct Error1BGB
24 Jan 25 i i +* Re: Struct Error4David Brown
24 Jan 25 i i i`* Re: Struct Error3Kaz Kylheku
24 Jan 25 i i i +- Re: Struct Error1bart
25 Jan 25 i i i `- Re: Struct Error1James Kuyper
24 Jan 25 i i `* Re: Struct Error2James Kuyper
25 Jan 25 i i  `- Re: Struct Error1bart
24 Jan 25 i +* Re: Struct Error5Lawrence D'Oliveiro
24 Jan 25 i i`* Re: Struct Error4bart
24 Jan 25 i i `* Re: Struct Error3Lawrence D'Oliveiro
24 Jan 25 i i  +- Re: Struct Error1Keith Thompson
24 Jan 25 i i  `- Re: Struct Error1bart
24 Jan 25 i +- Re: Struct Error1James Kuyper
24 Jan 25 i `* Re: Struct Error10Michael S
26 Jan 25 i  `* Re: Struct Error9bart
26 Jan 25 i   +- Re: Struct Error1Michael S
27 Jan 25 i   +* Re: Struct Error2Kaz Kylheku
27 Jan 25 i   i`- Re: Struct Error1bart
29 Jan 25 i   `* Re: Struct Error5Tim Rentsch
29 Jan 25 i    +* Re: Struct Error2bart
30 Jan 25 i    i`- Re: Struct Error1Tim Rentsch
29 Jan 25 i    `* Re: Struct Error2Richard Damon
29 Jan 25 i     `- Re: Struct Error1Tim Rentsch
23 Jan 25 +- Re: Struct Error1Tim Rentsch
3 Feb 25 `- Re: Struct Error1Andrey Tarasevich

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal