有关const char到char的问题。。。

jassically 2012-04-17 12:43:37
代码很简单,来源于《c++程序设计》,谭浩强版。。

#include<iostream>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};

int main()
{
void print(Student);
Student stu;
stu.num=12345;
stu.name="TanHaoQiang";
stu.score[0]=60;
stu.score[1]=70;
stu.score[2]=80;
print(stu);
system("pause");
return 0;
}

void print(Student stu)
{
cout<<stu.num<<stu.name<<stu.score[0]<<stu.score[1]<<stu.score[]2<<endl;
}


这是编译报的错。。。15 E:\dev-c++\DEV-CPP\程序\书例7-5.cpp incompatible types in assignment of `const char[12]' to `char[20]'

我将char name[20]改成string name。。程序就能运行了。。。。

我想知道一下,用char的话要怎么改源程序才行???谢谢喽。。。。
...全文
903 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
jassically 2012-04-18
  • 打赏
  • 举报
回复
谢谢喽。。。不过main前面的那句声明不需要吧。。。[Quote=引用 12 楼 的回复:]
C/C++ code

#include<iostream>
#include <string>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};
void print(Student stu);
int main()
{
void print……
[/Quote]
jassically 2012-04-18
  • 打赏
  • 举报
回复
那有木有推荐的书呢????[Quote=引用 16 楼 的回复:]
引用 10 楼 的回复:

把这句话提前到main函数???显然没用。。。还是报const char to char的问题。。汗死。。。引用 8 楼 的回复:
#include<iostream>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};

void pr……
[/Quote]
李荣强 2012-04-18
  • 打赏
  • 举报
回复
呵呵,听楼上的说,我有点庆幸没有用过浩强哥的书,不过c++入门,本人强力推荐范磊老师的零起点学通c++
这个视频那是一个经典啊,如果喜欢的话,百度一下吧!
希望对楼主有用
jassically 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
楼主,你个地方也错了:stu.score[]2<<endl;应该是 stu.score[2]吧
[/Quote]

改完还是报错呢。。。。汗死了。。。。
jassically 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
楼主,你个地方也错了:stu.score[]2<<endl;应该是 stu.score[2]吧
[/Quote]
额。。。这个打错了。。。囧了。。。。
jassically 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
stu.name="TanHaoQiang"; ==》strcpy (stu.name,"TanHaoQiang"); 这样也可以的。
[/Quote]

其他的不用改嘛?
hen_hao_ji 2012-04-17
  • 打赏
  • 举报
回复
楼主,你个地方也错了:stu.score[]2<<endl;应该是 stu.score[2]吧
hen_hao_ji 2012-04-17
  • 打赏
  • 举报
回复
要用 strcpy(stu.name, "TanHaoQiang");拷贝函数才行。。
hideforever 2012-04-17
  • 打赏
  • 举报
回复
strcpy(stu.name, "TanHaoQiang");
dengxin1985 2012-04-17
  • 打赏
  • 举报
回复
stu.name="TanHaoQiang"; ==》strcpy (stu.name,"TanHaoQiang"); 这样也可以的。
iamnobody 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

把这句话提前到main函数???显然没用。。。还是报const char to char的问题。。汗死。。。引用 8 楼 的回复:
#include<iostream>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};

void print(Student);
……
[/Quote]

这个还以为你知道了呢,如一楼所说.应该用strcpy

stu.name = "xxx"; 这个用法是错的.

还有,如果你还有点志向就别看他的书...

李荣强 2012-04-17
  • 打赏
  • 举报
回复
因为数组名是个常量,所以不能赋值,你把char name[20],改为char *name;就可以了,希望对你有用
hackbuteer1 2012-04-17
  • 打赏
  • 举报
回复
只能使用strcpy 函数来拷贝的,char 数组不能使用 赋值号来进行赋值操作的
一根烂笔头 2012-04-17
  • 打赏
  • 举报
回复

#include<iostream>
#include <string>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};
void print(Student stu);
int main()
{
void print(Student);
Student stu;
stu.num=12345;
strcpy(stu.name,"谭浩强比较坑爹");
stu.score[0]=60;
stu.score[1]=70;
stu.score[2]=80;
print(stu);
system("pause");
return 0;
}

void print(Student stu)
{
cout<<stu.num<<stu.name<<stu.score[0]<<stu.score[1]<<stu.score[2]<<endl;
}


一根烂笔头 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
#include<iostream>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};

void print(Student);
int main()
{
[/Quote]

++
函数没有在main之前声明,你无法调用定义在main之后的函数
jassically 2012-04-17
  • 打赏
  • 举报
回复
把这句话提前到main函数???显然没用。。。还是报const char to char的问题。。汗死。。。[Quote=引用 8 楼 的回复:]
#include<iostream>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};

void print(Student);
int main()
{
[/Quote]
一根烂笔头 2012-04-17
  • 打赏
  • 举报
回复
stu.name="TanHaoQiang";
这里不对,name是个数组是个const char *常量,不能够修改其值,执行赋值操作是非法的。可以调用string库中函数strcpy函数来拷贝。
iamnobody 2012-04-17
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
struct Student
{
int num;
char name[20];
float score[3];
};

void print(Student);
int main()
{

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧