Sujet : Re: Struct Error
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 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/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca