通过指针间接地修改结构体结构成员内容
在书上看到一个例子,实验一下调试不通,不清除错到哪里了?刚刚学习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[];这样的声明也不是很理解。
[解决办法]
太复杂的代码.
用c++ primer学习c++吧.
ptr-> woman[0]= 'C ';
[解决办法]
换书,C++ Primer
[解决办法]
char man[];
编译器不能确定数组的大小.
void break_up(struct couple *ptr)
{
ptr-> woman= 'C ';//这里不对,因为woman是数组,所以应该是字符串类型的
}