Sujet : Re: Loops (was Re: do { quit; } else { })
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 14. May 2025, 04:35:27
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250513202557.281@kylheku.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-05-13, Keith Thompson <Keith.S.Thompson+
u@gmail.com> wrote:
is valid, because, for example, the last 5 elements of a 10-element
array object can be treated as a 5-element array object. gcc seems
to agree, based on the fact that it warns about func(arr+6) but
not about func(arr+5).
>
This is a fundamental part of my mental model of C, but in a few
minutes of searching I wasn't able to find explicit wording in the
standard that supports it.
I haven't found explicit wording which supports the concept in the above use
scenario.
The Library clause (I'm referring to n3301 here0 has the wording:
If a function argument is described as being an array, the pointer passed to
the function shall have a value such that all address computations and
accesses to objects (that would be valid if the pointer did point to the first
element of such an array) are valid.(218)
Footnote 218 gives some degenerate examples more extreme than the
arr + 5 situation:
This includes, for example, passing a valid pointer that points
one-past-the-end of an array along with a size of 0, or using any valid
pointer with a size of 0.
The presence of the wording in the Library clause suggests that someone though
it was necessary. It needs to be extended (or repeated elsewhere with adjusted
wording) to cover situations involving language constructs that are not
standard library functions.
Progarms that manipulate strings using standard library functions often
take advantage of the above. Strings are defined as null-terminated
arrays; but it is very common for strings to be arrays that are displaced
within larger arrays.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca