Machine-translated by Google. Questions are authored in English.
Top

Discussion

Can you combine the following two statements into one?

char *p;
p = (char*) malloc(100);

 

  • A.char p = *malloc(100);
  • B.char *p = (char) malloc(100);
  • C.char *p = (char*)malloc(100);
  • D.char *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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.