Can someone please tell me what this code does?
void f(int *x)
{
x=(int*)malloc(sizeof(int));
*x=12;
}
int main(){
int v=10;
f(&v);
printf("%d\n",v);
return 0;
}
It return 10
Name:
Anonymous2012-02-03 9:02
you pass a reference to a symbol v that is known as x in another scope, then you modify the value of x (but not the value referenced by v), so naturally v stills references to 10
Name:
Anonymous2012-02-03 9:05
basically your use of malloc is retarded since local symbols are already allocated on the stack and x needs not be persistant