结构数组复制如何在 C 中工作?
How does structure array copying work in C?
这是我想要在 C 中 运行 的代码。
#include<stdio.h>
#include<string.h>
main()
{
struct record {
char name[2];
char letter;
};
struct record student[10];
strcpy(student[0].name,"t");//copy "t" to first struct's name variable
strcpy(student[1].name,"ri");//copy "ri" to second struct's name variable
student[0].letter='a';//copy "a" to first struct's letter variable
student[1].letter='b';//copy "b" to second struct's letter variable
printf("%s %s %c %c", student[0].name, student[1].name, student[0].letter, student[1].letter);
}
我期望的输出是:t ri a b
但是我得到:t rib a b
我做错了什么?
strcpy(student1.name,"ri");//copy "ri" to second struct's name
variable
这绝对是错误的。 "ri"
实际上也是三个字符,因为空终止符也是如此。因此,您将三个字节复制到具有 2 个字节的数组,从而调用 undefined behaviour。
在您的代码中,name
是一个包含两个 char
的数组。 OTOH,"ri"
的大小是三个字符,包括空终止符。
所以,通过说
strcpy(student[1].name,"ri");
你超出了内存。这会调用 undefined behavior.
来自man page
[...] The strings may not overlap, and the destination string dest
must be large enough to receive the copy. [...] If the destination string of a strcpy()
is not large enough, then anything might happen.
一旦命中UB,程序行为就无法自圆其说了。
为了能够保持"ri"
,你需要改变
char name[2];
到
char name[3];
#include<stdio.h>
#include<string.h>
main()
{
struct record {
char name[3];
char letter;
};
struct record student[10];
strcpy(student[0].name,"t");//copy "t" to first struct's name variable
strcpy(student[1].name,"ri");//copy "ri" to second struct's name variable
student[0].letter='a';//copy "a" to first struct's letter variable
student[1].letter='b';//copy "b" to second struct's letter variable
printf("%s %s %c %c",student[0].name,student[1].name,student[0].letter,student[1].letter);
}
这是我想要在 C 中 运行 的代码。
#include<stdio.h>
#include<string.h>
main()
{
struct record {
char name[2];
char letter;
};
struct record student[10];
strcpy(student[0].name,"t");//copy "t" to first struct's name variable
strcpy(student[1].name,"ri");//copy "ri" to second struct's name variable
student[0].letter='a';//copy "a" to first struct's letter variable
student[1].letter='b';//copy "b" to second struct's letter variable
printf("%s %s %c %c", student[0].name, student[1].name, student[0].letter, student[1].letter);
}
我期望的输出是:t ri a b
但是我得到:t rib a b
我做错了什么?
strcpy(student1.name,"ri");//copy "ri" to second struct's name variable
这绝对是错误的。 "ri"
实际上也是三个字符,因为空终止符也是如此。因此,您将三个字节复制到具有 2 个字节的数组,从而调用 undefined behaviour。
在您的代码中,name
是一个包含两个 char
的数组。 OTOH,"ri"
的大小是三个字符,包括空终止符。
所以,通过说
strcpy(student[1].name,"ri");
你超出了内存。这会调用 undefined behavior.
来自man page
[...] The strings may not overlap, and the destination string
dest
must be large enough to receive the copy. [...] If the destination string of astrcpy()
is not large enough, then anything might happen.
一旦命中UB,程序行为就无法自圆其说了。
为了能够保持"ri"
,你需要改变
char name[2];
到
char name[3];
#include<stdio.h>
#include<string.h>
main()
{
struct record {
char name[3];
char letter;
};
struct record student[10];
strcpy(student[0].name,"t");//copy "t" to first struct's name variable
strcpy(student[1].name,"ri");//copy "ri" to second struct's name variable
student[0].letter='a';//copy "a" to first struct's letter variable
student[1].letter='b';//copy "b" to second struct's letter variable
printf("%s %s %c %c",student[0].name,student[1].name,student[0].letter,student[1].letter);
}