我也有问题啊
以下这个是OK的。
#include <iostream>
using namespace std;
int fun(int p)
{
int i,sum=0;
for(i=0;i<=p;i++)
{
sum+=i;
}
return sum;
}
int main()
{
int a,s=0;
int sum=0;
cout << "Please enter number:";
cin >> s;
cout << "Fun=" << fun(s) << endl;
}
但这个就有问题了,编译通过,但是运行时出现故障。是怎么回事啊?
#include <iostream>
using namespace std;
int fun(int p)
{
int i,sum=0;
for(i=0;i<=p;i++)
{
sum+=i;
}
return sum;
}
int main()
{
int a,*s=0;
int sum=0;
cout << "Please enter number:";
cin >> *s;
cout << "Fun=" << fun(*s) << endl;
}
这个只是把指针移到子函数,但是同样出现错误,编译出错:error C2664: 'fun' : cannot convert parameter 1 from 'int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
#include <iostream>
using namespace std;
int fun(int *p)
{
int i,sum=0;
for(i=0;i<=*p;i++)
{
sum+=i;
}
return sum;
}
int main()
{
int a,s=0;
int sum=0;
cout << "Please enter number:";
cin >> s;
cout << "Fun=" << fun(s) << endl;
}
等待高手明示,谢谢!