& C 中数组的运算符定义
最近的一个问题引发了一场围绕数组和指针的讨论。该问题涉及 scanf("%s", &name)
与 scanf("%s", name)
。
对于以下代码,微软实际上在 VS2010(也许是早期版本?)中为您解决了这个问题,
#include <stdio.h>
int main()
{
char name[30];
printf("Scan \"name\" - ");
scanf("%s", name);
printf("Print \"&name\" - %s\n", &name);
printf("Print \"name\" - %s\n", name);
printf("Pointer to &name - %p\n", &name);
printf("Pointer to name - %p\n", name);
printf("\n\n");
printf("Scan \"&name\" - ");
scanf("%s", &name);
printf("Print \"&name\" - %s\n", &name);
printf("Print \"name\" - %s\n", name);
printf("Pointer to &name - %p\n", &name);
printf("Pointer to name - %p\n", name);
return 0;
}
这实际上是在 ANSI C 标准中定义的,还是允许依赖于编译器?这是否有效,因为 MS 将所有内容都视为 C++?请暂时忽略缓冲区溢出问题。
A recent question prompted a discussion centering on arrays and pointers. The question was in reference to scanf("%s", &name)
vs scanf("%s", name)
.
For the following code, Microsoft actually resolves this for you in VS2010 (and maybe earlier versions?),
#include <stdio.h>
int main()
{
char name[30];
printf("Scan \"name\" - ");
scanf("%s", name);
printf("Print \"&name\" - %s\n", &name);
printf("Print \"name\" - %s\n", name);
printf("Pointer to &name - %p\n", &name);
printf("Pointer to name - %p\n", name);
printf("\n\n");
printf("Scan \"&name\" - ");
scanf("%s", &name);
printf("Print \"&name\" - %s\n", &name);
printf("Print \"name\" - %s\n", name);
printf("Pointer to &name - %p\n", &name);
printf("Pointer to name - %p\n", name);
return 0;
}
Is this actually defined in the ANSI C Standard, or is this allowed to be compiler dependent? Does this work because MS is treating everything as C++? Please ignore buffer overflow issues for now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
name
和&name
应该给出相同的结果。严格来说,根据 C 语言标准,只有name
有效,并且&name
会导致未定义的行为,因此您绝对应该使用name
,但是实际上两者都会起作用。name
是一个数组,因此当您将它用作函数的参数时(就像将其传递给printf
时所做的那样),它会“衰减”为指向它的初始元素(这里是一个char*
)。&name
给出数组的地址;该地址与初始元素的地址相同(因为数组的初始元素之前或数组中的元素之间不能有填充字节),因此&name
和name
具有相同的指针值。但是,它们具有不同的类型:
&name
的类型为char (*)[30]
(指向 30 个char
数组的指针) )而name
,当它衰减为指向其初始元素的指针时,其类型为char*
(指向char
的指针,在此case,数组的初始char
元素名称
)。由于它们具有相同的值,并且
printf
和scanf
函数将参数重新解释为char*
,因此无论您是否传递name
或&name
。Both
name
and&name
should give the same result. Strictly speaking, onlyname
is valid per the C language standard and&name
results in undefined behavior, so you should definitely usename
, but in practice both will work.name
is an array, so when you use it as an argument to a function (as you do when you pass it toprintf
), it "decays" to a pointer to its initial element (which is achar*
here).&name
gives you the address of the array; this address is the same as the address of the initial element (because there can be no padding bytes before the initial element of an array or between elements in an array), so&name
andname
have the same pointer value.However, they have different types:
&name
is of typechar (*)[30]
(a pointer to an array of 30char
) whilename
, when it decays to a pointer to its initial element, is of typechar*
(a pointer to achar
, in this case, the initialchar
element of the arrayname
).Since they have the same value and since the
printf
andscanf
functions reinterpret the argument as achar*
anyway, there should be no difference whether you passname
or&name
.根据标准未定义的行为。
printf 转换说明符
"%p"
需要一个void*
:其他任何内容都会调用 UBprintf 转换说明符
"%s"
需要一个char*
,其中包含指向对象内某处的空字节:其他任何内容都会调用 UBscanf 转换说明符
"%s"
需要一个具有足够输入空间的char*
和一个额外的 null 终止字节:其他任何内容都会调用 UB如果任何实现定义了该行为,那么在该实现中使用应该可以。
最常使用
printf("%p" 打印
导致 UB 表现与预期行为无法区分。char*
或char(*)[30]
而不是void*
")大多数情况下,使用
printf("%s")
打印char(*)[30]
而不是char*
会导致 UB 表现这与预期的行为没有区别。从你的说法来看,在这些情况下,MS 的 UB 表现与预期的相同。
但这仍然是未定义的行为。
Undefined Behaviour as per the Standard.
The printf conversion specifier
"%p"
expects avoid*
: anything else invokes UBThe printf conversion specifier
"%s"
expects achar*
that includes a null byte somewhere inside the object pointed to: anything else invokes UBThe scanf conversion specifier
"%s"
expects achar*
with enough space for the input and an extra null terminating byte: anything else invokes UBIf any implementation defines the behaviour, then it should be ok to use in that implementation.
Most often printing a
char*
or achar(*)[30]
instead of avoid*
withprintf("%p")
results in a UB manifestation that is indistinguishable from the intended behaviour.Most often printing a
char(*)[30]
instead of achar*
withprintf("%s")
results in a UB manifestation that is indistinguishable from the intended behaviour.From what you say, it appears MS manifestation of UB in these cases is the same as intended.
But it's still Undefined Behaviour.