|
From: Lloyd <ll...@cd...> - 2007-05-21 08:48:31
|
Hi ,
This is a small program and its output
#include<iostream>
using namespace std;
bool call(int *a)
{
static int x;
++x;
int *temp=new int;
*temp=x;
a=temp;
cout<<"call->"<<x<<","<<*a<<endl;
return true;
}
int main()
{
int *a;
for(int i=0;i<10;++i)
{
call(a);
cout<<"val->"<<*a<<endl;
}
return 0;
}
Outputcall->1,1
val->0
call->2,2
val->0
call->3,3
val->0
......
...
The output which I wanted is
Outputcall->1,1
val->1
call->2,2
val->2
call->3,3
val->3
.....
....
The modification of the pointer in the function is reflected in the
calling function. Would anybody tell me the reason for this mistake?
Thanks and Regards,
Lloyd
______________________________________
Scanned and protected by Email scanner
|