-
Consider the following three C functions
[P1] int*g(void)
{
intx = 10;
return (&x);
}
[P2] int*g(void)
{
int* px;
*px = 10;
return px;
}
[P3] int*g(void)
{
int*px
px = (int*) malloc (size of (int));
*px = 10;
return px;
}
Which of the above 3 functions are likely to cause problems with pointers ?
-
- Only P3
- P1 and P3
- P1 and P2
- P1, P2 and P3
- Only P3
Correct Option: C
P1 : Here the function is returning address of the variable x (& x) but the return type is pointer to integer not address. So incorrect.
P2 : *px = 0 directly assigned a value but still px doesn't point to any memory location, so memory initialization or allocation should be done before. So incorrect.
P3 : Correction made in P2, memory pre allocated, So correct.
Hence (c) is correct option.