Liste des Groupes | Revenir à l c |
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>On 12.05.2025 17:08, David Brown wrote:>
>On 11/05/2025 23:43, James Kuyper wrote:>
[...]
>>More precisely, it makes it undefined behavior for values to point to an>
array of less than 10 doubles.
The wording of the C standard (C11, as that's what I have open at the
moment) is :
>
"""
If the keyword static also appears within the [ and ] of the array type
derivation, then for each call to the function, the value of the
corresponding actual argument shall provide access to the first element
of an array with at least as many elements as specified by the size
expression.
"""
Oh! - This is somewhat surprising to me. - When I first read about
the "arr[static N]" I assumed that it would be possible to pass any
sub-array, say "&arr[8]" (or "arr+8") as long as it's large enough
to still have N elements from the starting point, but the quote says
explicitly "access to the _first_ element of an array" (which I'd
interpret as "&arr[0]" (or "arr"). - Unless I misinterpreted that;
what would be the reason for that?
My personal interpretation is that this:
>
void func(int arr[static 5]) {
}
>
int main(void) {
int arr[10];
func(arr+5); // OK
// func(arr+6); // UB
}
>
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.
Les messages affichés proviennent d'usenet.