LeoHsiao1 2016-08-24 05:22 采纳率: 100%
浏览 1068
已采纳

给字符串数组型结构成员赋值失败

#include
struct horse{
char name[10];
int age;
};
void main(){
struct horse list;
list.name[10]="Leo";
printf("The name is %s.\n",list.name);
}
如上,平台是VC6.0,我想测试给list.name[10]赋值的各种方法,用scanf可以赋值,但是直接用=后却输出一个垃圾值,也就是赋值失败。为此请教!

  • 写回答

1条回答 默认 最新

  • 小灸舞 2016-08-24 05:40
    关注

    首先,你这个用法就是错的,你这样是在对name的第11个字符进行操作
    而且字符串赋值要用strcpy才行,不是用=

     #include <stdio.h>
    #include<string.h>
    struct horse{
        char name[10];
        int age;
    };
    void main(){
        struct horse list;
        strcpy(list.name, "Leo");
        printf("The name is %s.\n", list.name);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?