单个数组元素会衰减为指针吗?

Does individual array elements decay to pointer?

我知道数组会衰减为指针。因此,因为它的行为类似于指针,所以它基本上可以传递给带有需要内存地址的参数的函数,而无需使用 & 符号。例如:

char name[10];
scanf("%s", name);

正确。

但是,假设我只想填写'name'数组的第一个元素,为什么我不能把它写成

scanf("%c", name[0]);

但是一定要在'name[0]'前面加上符号?这是否意味着单个数组元素不会衰减为指针并且不保存它们的单个内存地址而不是它们的整个数组对应物?

数组 在作为函数参数传递时衰减为指向第一个元素的指针,而不是数组元素.

引用 C11,章节 §6.3.2.1/ p3,左值、数组和函数指示符,(强调我的

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

数组元素不是数组类型,数组变量是。换句话说,毕竟数组名称与 (ny) 数组元素不同。

因此,单个数组元素不会衰减为指向任何东西的指针,即使作为函数参数传递也是如此。

问题是 char name[10] 可以表现为 char* name,这对于 scanf 是可以的, 期望第二个参数是指针 (内存中的地址)。但是当你写 name[0] 时,你得到的是值而不是指针。

例如,如果 name"Hello world",则 name[0] == 'H'。但是 scanf 想要一个指针。所以为了得到name[0]的地址你需要写成scanf("%c", &name[0]).