& C 中数组的运算符定义

发布于 2024-11-04 22:16:00 字数 872 浏览 3 评论 0原文

最近的一个问题引发了一场围绕数组和指针的讨论。该问题涉及 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聚集的泪 2024-11-11 22:16:00

name&name 应该给出相同的结果。严格来说,根据 C 语言标准,只有 name 有效,并且 &name 会导致未定义的行为,因此您绝对应该使用 name,但是实际上两者都会起作用。

name 是一个数组,因此当您将它用作函数的参数时(就像将其传递给 printf 时所做的那样),它会“衰减”为指向它的初始元素(这里是一个 char*)。

&name 给出数组的地址;该地址与初始元素的地址相同(因为数组的初始元素之前或数组中的元素之间不能有填充字节),因此 &namename 具有相同的指针值。

但是,它们具有不同的类型:&name 的类型为 char (*)[30](指向 30 个 char 数组的指针) )而 name,当它衰减为指向其初始元素的指针时,其类型为 char*(指向 char 的指针,在此case,数组的初始 char 元素名称)。

由于它们具有相同的值,并且 printfscanf 函数将参数重新解释为 char*,因此无论您是否传递 name&name

Both name and &name should give the same result. Strictly speaking, only name is valid per the C language standard and &name results in undefined behavior, so you should definitely use name, 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 to printf), it "decays" to a pointer to its initial element (which is a char* 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 and name have the same pointer value.

However, they have different types: &name is of type char (*)[30] (a pointer to an array of 30 char) while name, when it decays to a pointer to its initial element, is of type char* (a pointer to a char, in this case, the initial char element of the array name).

Since they have the same value and since the printf and scanf functions reinterpret the argument as a char* anyway, there should be no difference whether you pass name or &name.

一杯敬自由 2024-11-11 22:16:00

根据标准未定义的行为。

printf 转换说明符 "%p" 需要一个 void*:其他任何内容都会调用 UB
printf 转换说明符 "%s" 需要一个 char*,其中包含指向对象内某处的空字节:其他任何内容都会调用 UB
scanf 转换说明符 "%s" 需要一个具有足够输入空间的 char* 和一个额外的 null 终止字节:其他任何内容都会调用 UB

如果任何实现定义了该行为,那么在该实现中使用应该可以。

最常使用 printf("%p" 打印 char*char(*)[30] 而不是 void* ") 导致 UB 表现与预期行为无法区分。

大多数情况下,使用 printf("%s") 打印 char(*)[30] 而不是 char* 会导致 UB 表现这与预期的行为没有区别。

从你的说法来看,在这些情况下,MS 的 UB 表现与预期的相同。

但这仍然是未定义的行为。

Undefined Behaviour as per the Standard.

The printf conversion specifier "%p" expects a void*: anything else invokes UB
The printf conversion specifier "%s" expects a char* that includes a null byte somewhere inside the object pointed to: anything else invokes UB
The scanf conversion specifier "%s" expects a char* with enough space for the input and an extra null terminating byte: anything else invokes UB

If any implementation defines the behaviour, then it should be ok to use in that implementation.

Most often printing a char* or a char(*)[30] instead of a void* with printf("%p") results in a UB manifestation that is indistinguishable from the intended behaviour.

Most often printing a char(*)[30] instead of a char* with printf("%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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文