[OT] basic detail
Tue Feb 19 17:34:00 GMT 2019
在 2019/2/19 23:11, Xi Ruoyao 写道:
> On 2019-02-19 15:44 +0100, Patrick Bégou wrote:
>> I need a small theoritical explanation about arrays in C.
> These questions are off-topic in gcc-help. It should go to comp.lang.c or
>> I would like to know why using:
>> char A;
>> scanf("%s", A); and scanf("%s", &A); does the same thing (as it is not
>> the same code).
>> I've checked that printing A or &A with "%p" prints the same address....
>> but why ?
> STFG. The first URL Google suggested for "address of array" is exactly the
> answer of this question:
Converting both `A` and `&A` to `void *` will yield the same result,
because 'an array type describes a contiguously allocated nonempty set
of objects with a particular member object type, called the /element
type/' . There have to be nothing else; not even padding bytes are
allowed. However, as function arguments, the former decays to `char *`
while the latter has exact `char (*)`.
`%s` requires a corresponding argument be 'a pointer to the
initial element of a character array' , where `A` satisfies this
requirement, so it is perfectly valid there, albeit unsafe.
Passing `&A` as the argument corresponding to either `%s` for `scanf()`
or `%p` for `printf()` results in undefined behavior because `char
(*)` may have a representation from `char *` , which GCC warns
about if `-Wpedantic` is used with `-Wformat` or `-Wall`.
 ISO/IEC WG14 Draft N2176, 6.2.5 Types, 20
 ISO/IEC WG14 Draft N2176, 18.104.22.168 The fscanf function, 12
More information about the Gcc-help