问个很简单问题,似乎是codeblocks问题
codeblocks吧
全部回复
仅看楼主
level 1
瞧瞧缩头 楼主
//下面代码中全局变量 i,j 在cb中运行结果为0,我在网页在线编译器可以显示正常数据为10 有人知道这是bug吗
#include <iostream
using namespace std;
int i=0,j=0;
int whatisthis(int [],int);
int whatis(int [],int);int main(void)
{
int maxsize=10;
int ac[maxsize]={1,2,3,4,5,6,7,8,9,10};
cout<<"whatisthis="<<whatisthis(ac,maxsize)<<" "<<i<<endl;
cout<<"whatis="<<whatis(ac,maxsize-1)<<" "<<j<<endl;
return 0;
}
int whatisthis(int a[],int maxsize)
{ if(maxsize==1)
{
i++;
return a[0];
}
else
{
i++;
return a[maxsize-1]+whatisthis(a,maxsize-1);
}
}
int whatis(int a[],int maxsize)
{ if(maxsize==0)
{
j++;
return a[0];
}
else
{
j++;
return a[maxsize]+whatis(a,maxsize-1);
}
}
2017年10月31日 16点10分 1
level 5
经测试 GCC 编译 结果都是0
VC2010 编译器 DEBUG版是0 Release版是10
楼主代码使用了未定义的写法了吧
\
2017年11月01日 04点11分 2
level 1
瞧瞧缩头 楼主
单文件全局变量是不是这样写法,这样引用
2017年11月01日 04点11分 3
level 5
看到你在 csdn的帖子了
cout 分2行 ,2条语句 就正确了
2017年11月01日 11点11分 4
1