MY mENU


Tuesday 3 April 2012

When should a type cast be used?


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));
36.What is a null pointer? There are times when it's necessary to have a pointer that doesn't point to anything. The macro NULL, defined in , has a value that's guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather than NULL. The null pointer is used in three ways:
 1) To stop indirection in a recursive data structure 
 2) As an error value
 3) As a sentinel value.

No comments:

Post a Comment