Sujet : Re: Struct Error
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 27. Jan 2025, 21:19:13
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vn8po2$172j1$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
On 27/01/2025 04:05, Kaz Kylheku wrote:
On 2025-01-26, bart <bc@freeuk.com> wrote:
On 24/01/2025 14:37, Michael S wrote:
This is a pointer to an incomplete type. Attempts to do ++ptr for
example will fail later on if that struct has not yet been defined.
>
So why not the same for the pointer-to-array versions?
>
It just doesn't make sense.
You already know that GNU C++ silently accepts it, so this is
beating a dead horse.
C++ is no good to me. My old transpiler (now a deprecated product anyway) generated C. Fixing it either means updating the transpiler (and finding some hacky workaround like using casts everywhere) or doing that manually to the generated C. Neither appeal.
Your situation is this:
struct incomplete {
struct incomplete (*parray)[];
};
If we make a pointer to a struct rather than array,
it's the same kind of problem:
struct incomplete {
struct nested_incomplete {
struct incomplete memb;
} *pstruct;
};
In both cases, we have a pointer to something which
has an element, or member, of the incomplete type of
the outer struct which is to contain the pointer.
It's trickier problem: I'm not sure myself what the size should be, whereas that was easy to see with my array example. Here even TCC reports it as incomplete. How my C compiler tells me the size is 8 bytes, which sounds reasonable given that the only concrete member in there is one 64-bit pointer.
If the array version should work, so should the
struct version.
The case of the recursive structure is special only in a sense that it's
o.k. in C++, because [unlike C] in C++ struct considered complete within
its own body.
>
For non-recursive, you can choose to declare the pointer-to-array after
the struct has been fully defined.
If a C++ struct is complete within its own body, that means this should
be possible:
struct foo {
struct foo x;
int y;
};
This one seems impossible, and even C++ fails it. Because you're directly including an actual struct within itself.
Still, my compiler is not bothered by it! It gives an overall size of 16 bytes and an offset for both x and y of 0. That embedded (incomplete) version of struct foo uses the wrong size.
A similar example in my language gives a recursion failure.
However, examples like the ones in OP are well-defined: the member involved is a single pointer of a fixed size.