菜鸟在此请教一个C++中关于指针的问题

来源:百度知道 编辑:UC知道 时间:2024/06/23 08:34:33
我的程序如下:#include <iostream.h>
void main()
{
struct course_work
{
char course1[20];
char course2[20];
struct course_work *ptr;
};
//声明course_work类型的结构数组
course_work semester[3]=
{{"Computer 101","Math 101",&semester[1]},
{"System 101","Math 102",&semester[2]},
{"Analysis 101","Calulus",&semester[0]}};
int i;
for(i=0;i<=2;i++)
{
cout<<"contents being pointed to: "
<<semester[i].ptr->course1<<"\n";
}
}
我觉得应该输出的结果是:
contents being pointed to: System 101
contents being pointed to: Analysis 101
contents being pointed to: Computer 101
但实际的输出结果却是:
contents being pointed to: Analysis 101
contents being pointed to:
contents being pointed to:
不光第一条和我想的不一样,后面两条甚至没有显示任何内容!
哪位高手帮我看

编译通过,VC6.0,结果如下
contents being pointed to: System 101
contents being pointed to: Analysis 101
contents being pointed to: Computer 101

不过建议结构定义不要放在main函数里面

我这里编译后输出是这样的啊:
contents being pointed to: System 101
contents being pointed to: Analysis 101
contents being pointed to: Computer 101

我用BCB5.5和VC6都试过了,很正常,你用的是什么编译器?再试几次看看

输出为:
contents being pointed to: System 101
contents being pointed to: Analysis 101
contents being pointed to: Computer 101
好象你机器有毛病!