Liste des Groupes | Revenir à cl c |
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:My interpretation matches yours. I can't find any indication in the standard of a definition of what an "array" actually means. It would be extraordinary - and render vast amounts of C code as having undefined behaviour - if "arr + 5" above could not be considered as pointing to the first element of an array of 5 int. (I believe the same logic applies to pointer arithmetic in general - not just this specific situation.) But that does not imply that the standard says a pointer to an element inside an array or array object can be treated as a pointer to the first element of an array and/or array object of appropriate size.On 12.05.2025 17:08, David Brown wrote:My personal interpretation is that this: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?
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.