为什么我们需要strcpy()?

Sam*_*aha -6 c c++

struct prac
{
    int name[3];
    char name1[12];
} b1, b2, c2;

main()
{
    int i;
    struct prac b2={1,2,3};

    strcpy(c2.name1,"goodmorning");

    printf("%s",c2.name1);
}
Run Code Online (Sandbox Code Playgroud)

这里将值存储在b2数组中我们只需要编写b2 = {1,2,3}但是如果我想在字符串c2中存储一些值我们需要调用strcpy(),为什么如果我写c2它会显示错误="goodmorning",而不是使用strcpy()?

Som*_*ude 7

因为您不能分配给数组,只能初始化它(在定义第二个 b2变量时执行)或复制到它(使用该strcpy调用时).