结构体中char类型赋值的常用两种措施:

在声明结构体变量时初始化,如——

#include "stdio.h"
struct stu{
int x;
char name[10];
};
int main(void){
struct stu s={8,"123"};//这样初始化
printf("%d %s\n",s.x,s.name);
return 0;

}


向数组里直接拷贝字符串,如——

#include "stdio.h"
#include "string.h"
struct stu{
int x;
char name[10];
};
int main(void){
struct stu s;
strcpy(s.name,"abcd");//向name拷贝字符串
s.x=128;
printf("%d %s\n",s.x,s.name);
return 0;
}

猜你喜欢

转载自blog.csdn.net/scc_722/article/details/80781387
今日推荐