通过指针间接地修改结构体结构成员内容

linleming 2007-01-29 10:06:56
在书上看到一个例子,实验一下调试不通,不清除错到哪里了?刚刚学习C++(没有C基础),请不要见笑。
#include<iostream>
using namespace std;
struct couple
{
char man[];
char woman[];
};
void break_up(struct couple *ptr);
int main()
{
struct couple Camastra={"John","Sara"};
struct couple *ptr;
ptr=&Camastra;
cout<<"Before break_up,Camstra couple was:\n";
cout<<Camastra.man<<""<<Camastra.woman<<"\n";
break_up(ptr);
cout<<"After break_up,Camstra couple is:\n";
cout<<Camastra.man<<""<<Camastra.woman<<"\n";
system("pause");
}
void break_up(struct couple *ptr)
{
ptr->woman='C';
}


char man[];这样的声明也不是很理解。
...全文
337 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
linleming 2007-01-30
  • 打赏
  • 举报
回复
to Gqy99007(gqy007):

???
zx_hxp_jw 2007-01-30
  • 打赏
  • 举报
回复
mark
Gqy99007 2007-01-30
  • 打赏
  • 举报
回复
有人骂
linleming 2007-01-30
  • 打赏
  • 举报
回复
to htqx(航天奇侠):
两年以前做过PB开发,但从来没弄过C.两年多没搞开发了,想回头再学点。

to taodm(taodm):

这本电子书我也下了,目前看的已经快看了一半了,还是决定看完再看那个了!先谢过!

谢谢大家,结帖!
lidongri 2007-01-30
  • 打赏
  • 举报
回复
char man[];
编译器不能确定数组的大小.

void break_up(struct couple *ptr)
{
ptr->woman='C';//这里不对,因为woman是数组,所以应该是字符串类型的
}
sarh2onacy 2007-01-30
  • 打赏
  • 举报
回复
楼主悟性很高,呵呵!
taodm 2007-01-30
  • 打赏
  • 举报
回复
换书,C++ Primer
htqx 2007-01-29
  • 打赏
  • 举报
回复
你自己都能修改嘛,不错.不像刚学的.
linleming 2007-01-29
  • 打赏
  • 举报
回复
ptr->woman[0]='C';
改为这个后,还需要把
char man[];
char woman[];
改成char man[5];
char woman[5];
才可以,是不是原来的写法也不对呀!!!???

htqx 2007-01-29
  • 打赏
  • 举报
回复
太复杂的代码.

用c++ primer学习c++吧.

ptr->woman[0]='C';

linleming 2007-01-29
  • 打赏
  • 举报
回复
在网上下载的一本电子图书《非C程序员C++入门自学教程》中写的!
todototry 2007-01-29
  • 打赏
  • 举报
回复
什么地方看的

65,186

社区成员

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

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