关于结构体类型字符变量的赋值问题#include iostreamusing namespace stdconst int N3struct student
关于结构体类型字符变量的赋值问题
#include <iostream>
using namespace std;
const int N=3;
struct student
{
int number;
char name[20];
char sex[20];
int age;
char major[20];
struct student *next;
};
int main()
{
struct student stu[N] ,*head,*p;
int i,num;
char maj[20];
head=stu;
stu[0].number=1;
stu[0].name={"Mrak"};
//这个会报warning:extend initializer lists only available with -std=c+11 or -std=gnu++11(enabled by default)
stu[0].sex={"Female"};
stu[0].age=19;
//这个会报error:assigning to an array from an initializer list 为什么会报错呢?如果直接初始化的话这样的话是可以的吧,为什么赋值就不行呢?
stu[0].major="CS";
stu[1].number=2;
stu[1].name="Steve";
stu[1].sex="Female";
stu[1].age=24;
stu[1].major="SE";
stu[2].number=3;
stu[2].name="Mao";
stu[2].sex="Female";
stu[2].age=20;
stu[2].major="NE";
stu[0].next=&stu[1];
stu[1].next=&stu[2];
stu[2].next=NULL;
p=head;
do
{
cout<<p->number<<","<<p->name<<","<<p->sex<<","<<p->age;
cout<<","<<p->major<<endl;
p=p->next;
}
while(p!=NULL);
cout<<endl;
}
新手大白
求大大们指教。。。 struct
[解决办法]
char 数组是一个容器, 不是一个指向.
你要往容器里头装, 拷贝, copy, strcpy.
[解决办法]
数组不能赋值,只能初始化
[解决办法]
strcpy(stu[0].name,"Mark")
