Which statment is true about the given code ?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a[5];
a = (int*) malloc(sizeof(int)*5);
free(a);
return 0;
}
Answer: B
Correct answer: Error: We cannot store address of allocated memory in a
int *a[5] declares an array of five pointers. An array name is not a modifiable lvalue, so a = ... is rejected by the compiler - nothing can be assigned to it.
The declaration the code seems to intend is int *a;, a single pointer, which could then hold the allocated address. The distinction between an array of pointers and a pointer is exactly the kind of declaration that repays reading from the inside out.
Please log in or register to report this question.