首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请赐教解决思路

2012-02-24 
请赐教#includeiostream#includeiomanipusing namespace stdstruct student{int idchar name[20]ch

请赐教
#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, "赋值");
[解决办法]

C/C++ code
#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; } 

热点排行