Sujet : Re: Struct Error
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 27. Jan 2025, 05:05:10
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250126193003.409@kylheku.com>
References : 1 2 3 4 5
User-Agent : slrn/pre1.0.4-9 (Linux)
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.
Sure, something in a type not being specified is not a problem until the
information is actually needed for something. We can think about
a lazy type evaluation system. Functional programming languages
tend to have them.
But note that the rule /is/ actually consistent among aggregates.
Both an array and struct are aggregates. The elements are to
an array roughly the same thing that members are to a struct.
A struct may not have members of incomplete type,
An array may not have elements of incomplete type.
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.
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;
};
That cannot be the reason why the pointer to array works in GNU C++.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca