Can you combine the following two statements into one?
char *p;
p = (char*) malloc(100);
Answer: C
Correct answer: char *p = (char*)malloc(100);
A declaration may carry an initialiser, so the declaration and the assignment combine directly: char *p = (char*)malloc(100);.
The other forms misplace the asterisks. (char)malloc(100) casts the returned pointer to a single character, and the last option is not valid syntax at all. It is worth adding that in C the cast is unnecessary - void * converts to any object pointer type implicitly, and omitting the cast can expose a missing stdlib.h that the cast would hide.
Please log in or register to report this question.