取数组地址赋值给指针后,与数组名/解引用指针等同是否符合C标准?始于何时?
First, let's clarify a small detail about your code snippet: int *p = &array; has a type mismatch—&array is a pointer to an array of 10 integers (int (*)[10]), not a pointer to a single integer. GCC might let this slide with a warning, but it's not strictly compliant. That said, the core behavior you're observing (where the address of the array, the array itself when decayed, and the first element all share the same numerical value) is indeed defined by the C standard.
Is This Behavior Part of the C Standard?
Yes, absolutely. Here's what the standard specifies:
- When an array name is used in most contexts (like assigning to a pointer), it "decays" to a pointer to its first element. So
arrayis equivalent to&array[0]in these scenarios. - Additionally, the address of the entire array (
&array) must have the same numerical address as the address of its first element (&array[0]). Even though these two pointers have different types (pointer-to-array vs pointer-to-element), their underlying memory addresses are identical.
This explains why you see p == array (ignoring the type mismatch for a moment)—the numerical value of &array matches the decayed array pointer. As for p == *p: if p holds &array, then *p refers to the array itself, which decays to &array[0]—so again, the same numerical address.
Which C Standard Version Introduced This?
This behavior has been part of the C standard since the very first standardized iteration: C89 (ANSI C, 1989). It was carried over unchanged to all subsequent versions (C99, C11, C17, and C23). The array decay rule and equivalence of the array's address to its first element's address are fundamental to how arrays operate in C.
Are There Any Implementations With Different Addresses?
In practice, no. All conforming C implementations must follow this rule. The standard explicitly mandates that the address of the array is identical to the address of its first element. There's no room for a conforming implementation to use distinct addresses here—doing so would violate the standard's requirements.
That said, non-conforming implementations (like some embedded systems compilers that take liberties with the standard for specific hardware constraints) might theoretically behave differently, but these are not considered valid C implementations under the official standard.
内容的提问来源于stack exchange,提问作者Lesto

