#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可以赋值,但是直接用=后却输出一个垃圾值,也就是赋值失败。为此请教!
给字符串数组型结构成员赋值失败
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
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); }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报