请赐教
#include<iostream>
#include<iomanip>
using namespace std;
struct student{
int id;
char name[20];
char gender;
int age;
};
int main()
{
student stu;
stu.id=1;
stu.name="davery";
stu.gender='f';
stu.age=22;
cout<<stu.id<<setw(10)<<stu.name<<stu.gender<<stu.age<<endl;
}
有错误,是char数组,不知道如何解决!
[解决办法]
stu.name="davery"; 不能这么赋值,要用strcpy
[解决办法]
将 stu.name="davery";改为strcpy(stu.name,"davery");
[解决办法]
可以用 strcpy(stu.name, "赋值");
[解决办法]
#include <iostream> #include <iomanip> using namespace std; struct student{ int id; char name[20]; char gender; int age; }; int main() { char *str = "davery"; student stu; stu.id=1; memset(stu.name,0,sizeof(char)*20); memcpy(stu.name,str,strlen(str)); stu.gender='f'; stu.age=22; cout <<stu.id <<setw(10) << stu.name <<setw(10) << stu.gender <<setw(10)<<stu.age <<endl; }