below is a code in c
void afunction(int *x)
{
x=new int;
*x=12;
}
int main()
{
int v=10;
afunction(&v);
cout<<v;
}
i thinkthe answer should be 12,but according to an answer given by one expert is 10.please explain.
below is a code in c
void afunction(int *x)
{
x=new int;
*x=12;
}
int main()
{
int v=10;
afunction(&v);
cout<<v;
}
i thinkthe answer should be 12,but according to an answer given by one expert is 10.please explain.
This is not a code in C.
Last edited by pinko_liberal; 02-24-2003 at 04:10 PM.
The one who says it cannot be done should never interrupt the one who is doing it.
You are on the wrong board. This is C++ code.
The correct answer is 10.
This new variable is gone at the end of this function call. You are not updating the initial pointer, you are allocating a new one, which the original variable does not point to. (For one, because it is not a pointer, but an actual int.)Code:void afunction(int *x) { x=new int; // new is a C++ keyword *x=12; //x is now a new integer, not the first }
Quzah.
Hope is the first step on the road to disappointment.
no, that wasn't c code.
also, nothing will happen when you try to assign a new address to the address of a variable. it'd be almost like doing:
and hoping that something extra-ordinary would occur.Code:void fisk(int kalas) { kalas = 5; } int main() { fisk(7); }