C程序产生奇怪的输出

3

在我学习C的过程中,我决定创建一个结构体(struct),以便能够提供其中鱼的大小。我的问题是,为什么当我写下这段小代码时:

#include <stdio.h>
#include <string.h>

struct fish
    {
        char catfish[9]; //reserve memory for 9 chars
        char goldfish[10]; //reserve memory for 10 chars
        char mackrel;
        char oldfish;
    };

int main()
{
    struct fish typeof_fish;

    strcpy(typeof_fish.catfish, "Big Fish\n");
    strcpy(typeof_fish.goldfish, "Small Fish\n");
    printf("%s\n", typeof_fish.catfish);

    return 0;


}

输出结果为 "Big Fish Small Fish"。请查看这里
但是,当我重新编写顶部代码并将char catfish [9];更改为char catfish [10]

#include <stdio.h>
#include <string.h>

struct fish
    {
        char catfish[10]; //reserve memory for 10 chars
        char goldfish[10]; //reserve memory for 10 chars
        char mackrel;
        char oldfish;
    };

int main()
{
    struct fish typeof_fish;

    strcpy(typeof_fish.catfish, "Big Fish\n");
    strcpy(typeof_fish.goldfish, "Small Fish\n");
    printf("%s\n", typeof_fish.catfish);

    return 0;


}

它生成了“大鱼”。在这里查看输出

感谢提前解答这个困惑的错误。


4
你的数组溢出了。例如,"Small Fish\n" 占用了 12 个字符。它无法适应一个只有10个字符的数组。 - WhozCraig
2个回答

8
你在使用strcpy函数将"Big Fish\n"复制到catfish[9]数组时没有留出足够的空间来存放空字符'\0'。该字符串长度为9个字符,这意味着你需要一个大小为10的缓冲区来存储空字符。
如果字符串缺少空字符,则输出结果是未定义的,因为程序无法知道字符串何时结束。

感谢您的解释。 - Prinzeono Key

1
当你执行第一次复制时,strcpy()总共复制了10个字节(9个来自字符串本身加上终止符)。由于你只在catfish中分配了9个字节,因此终止符进入了goldfish的第一个字节,然后在复制第二个字符串时被覆盖。因此,当你对catfish进行printf()时,它不会停在catfish的末尾,而是继续打印直到找到goldfish末尾的终止符。
在第二种情况下,你添加了足够的空间,以免覆盖终止符,因此当你打印时,它将按预期打印catfish的内容。

感谢您的详细解释。 - Prinzeono Key

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接