www.thecareerplus.com
The CareerPlus
Home Technical Resources Programming Variables and Data types
Thursday 09th September 2010
 
 
When should a type cast be used?

Discuss it!          

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)); A type cast should not be used to override a const or volatile declaration. Overriding
these type modifiers can cause the program to fail to run correctly. A type cast
should not be used to turn a pointer to one type of structure or data type into another.
In the
rare events in which this action is beneficial, using a union to hold the values
makes the programmer?s intentions clearer.

Discuss it!          

CrackTheIntervew.NET
Advertisement
 
Top! Top!