#include <iostream> using namespace std; #include <sctring> struct student{ // 定义构造体 int num; char name[10]; float score[4]; }; int main(){ student stud; //定义变量 stud.num = 123; //成员的引用 strcpy(stud.name , "xxx"); }
注意在给 char name[10] 赋值时 不能 stud.name = "xxx" ,不能把字符串赋给地址
给char name[10]赋值可以 如上代码所示
也可以 cin>>stud.name;
或者 cin.getline ( stud.name , 80) ;