void f(int * & a)
{
int * temp; temp = new int [1];
temp[0] = 2;
a = temp;
}
int main ()
{
int * v = NULL; v = new int[1];
v[0] = 1;
f(v); // input = pointer to an int
cout <<v[0]<<endl; // v[0] = 2
int v1[1] = {1};
f(v1); // input = pointer to an int
cout <<v1[0]<<endl; // v[1] = 1
}
Dear Experts, in both cases input is pointer to integer, but output is different. Why? Thanks
Because v is a pointer to dynamic memory and v1 is a pointer to static memory.
What compiler are you using? This should not compile.
In Visual Studio 2010 you get the following error on the line f(v1);
a reference of type "int *&" (not const-qualified) cannot be initialized with a value of type "int [1]"