Basic and Advance C Question:
Download Questions PDF

When should a type cast be used?

Answer:

There are two situations in which to use a type cast. The first use is to change the type of an operand to an arithmetic operation so that the operation will be performed properly.
The second case is to cast pointer types to and from void * in order to interface with functions that expect or return void pointers. For example, the following line type casts the return value of the call to malloc() to be a pointer to a foo structure.
struct foo *p = (struct foo *) malloc(sizeof(struct foo));

Download C Programming Interview Questions And Answers PDF

Previous QuestionNext Question
How can I search for data in a linked list?What is a null pointer in C?