Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Struct Error
Date: Wed, 29 Jan 2025 07:52:33 -0800
Organization: A noiseless patient Spider
Lines: 91
Message-ID: <86cyg51uta.fsf@linuxsc.com>
References: <20250124163740.00006281@yahoo.com> <86jzad28d5.fsf@linuxsc.com> <3cd9101a43de105e6aaa74614d05fcba5b8c093c@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Wed, 29 Jan 2025 16:52:34 +0100 (CET)
Injection-Info: dont-email.me; posting-host="ba8d9ca2da4b9314498ee7469992f70e"; logging-data="2572767"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NBqwa7YeUkhqkF6fGpKKgHSHIOgVCCXY="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:8KON5n3RaMX1MmyBwd8By2fyO+k= sha1:SKNwpwwyS4W8MNS9/ysLjvcY/70=
Xref: csiph.com comp.lang.c:390191
Richard Damon writes:
> On 1/29/25 5:59 AM, Tim Rentsch wrote:
>
>> bart writes:
>>
>>> On 24/01/2025 14:37, Michael S wrote:
>>>
>>>> On Thu, 23 Jan 2025 10:54:10 +0000
>>>> bart wrote:
>>>>
>>>>> On 23/01/2025 01:05, James Kuyper wrote:
>>>>>
>>>>>> On 2025-01-22, bart 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)[];
>>>>>>> };
>>>>>>
>>>>>> 6.7.6.2p2: "The element type shall not be an incomplete or
>>>>>> function type."
>>>>>>
>>>>>> I have many draft versions of the C standard. n2912.pdf, dated
>>>>>> 2022-06-08, says in 6.7.2.1.p3 about struct types that "... the
>>>>>> type is incomplete144) until immediately after the closing brace
>>>>>> of the list defining the content, and complete thereafter."
>>>>>>
>>>>>> Therefore, struct scenet is not a complete type until the closing
>>>>>> brace of it's declaration.
>>>>>
>>>>> Wouldn't this also be the case here:
>>>>>
>>>>> struct scenet *child;
>>>>> };
>>>>
>>>> Just to point out if it was not said already: the problem is not
>>>> related specifically to recursive structures. It applies to arrays
>>>> of incomplete types in all circumstances.
>>>>
>>>> struct bar;
>>>> struct bar (*bag)[]; // error
>>>> typedef struct bar (*bat)[]; // error
>>>
>>> I don't think anyone has yet explained why that is an error (other
>>> than C says it is), but not this:
>>>
>>> struct bar *ptr;
>>>
>>> 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?
>>
>> The question you should be asking is why did the original C
>> standards body make the rule they did?
>
> My guess is that it makes the simplest implementation of a C compiler
> much more complicated. While I don't think it has been explicited
> stated, one goal the original language, and apparently kept by the
> Standards Comittee, has been to make the language fairly simple to
> proceess to get working code. To optimize to make fast, might take
> more work, but to make your first complier for a system should be
> straight forward. I believe a C compiler can still be done with a
> single pass through the source code, with limited look ahead, and only
> the final "link" step needs to be able to handle large chunks of the
> program.
>
> Allowing the pointer to array time to be based on an incomplete type
> might make this goal harder.
Possibly. I suspect the question was never considered, simply
because it never came up. It's unusual even to have a pointer to
an array with unknown extent, and an array with an incomplete
element type is an even weirder beast. It's easy to believe that
the peculiar circumstances of the situation being asked about
just never occurred to anyone. Given that, the simple rule in
the C standard has an obvious and natural appeal.