answersLogoWhite

0

Yes. A type cast generally means that an object's representation is re-interpreted, however re-interpretation isn't always suitable so we must use conversion instead.

The most common use of type-casting is to re-interpret pointer variables. A pointer variables stores a memory address and its type determines how the object stored at that address is to be interpreted. However a pointer to void has no type, it can refer to an object of any type. But if we want to access that object through the pointer we must first cast the pointer to the appropriate type. This requires no conversion because the memory address stored in the pointer does not change, we're only changing how that address is interpreted.

Another use of type casts is when we have a word (multi-byte value) and we wish to access the individual bytes in the word. For this we can simply cast the type to an array of char. Again, this requires no conversion whatsoever because the representation remains the same, only its interpretation changes.

When the physical representation must change, we must use a type conversion rather than a type cast. For instance, if we wish to re-interpret a char variable as a word then we must create a new variable to store that new representation. In mixed-mode arithmetic we can use any combination of numeric types (such as int, float and double) but with complex expressions it can often be difficult to see what conversions are actually taking place. For example:

void f (int i, float f, double d) {

int x = i * f * d;

}

In the above example, the expression is evaluated as follows:

void f (int i, float f, double d) { double t1 = (double) f; // conversion from float to double

double t2 = t1 * d; // no conversion

double t3 = (double) i; // conversion from int to double

double t4 = t3 * t2; // no conversion

int x = (int) t4; // conversion from double to int

}

Had i, f and d been of the same type as x, then the expression x = i * f * d would have resulted in just one temporary variable holding the evaluation of f * d, and no conversions would be necessary. But as soon as we introduce conversions the number of temporary variables increases accordingly.

In some cases we may wish to control the conversions rather than rely on automatic conversion which always promotes types to the highest precision type required of the expression. For instance, if we were not interested in the fractional components of the float and double types, we can explicitly cast them:

void f (int i, float f, double d) {

int x = i * (int) f * (int) d;

}

In so doing we reduce the number of conversions and temporaries we require:

void f (int i, float f, double d) {

int t1 = (int) f; // conversion from float to int

int t2 = (int) d; // conversion from double to int

int t3 = f * d; // no conversion

int x = i * t3; // no conversion

}

User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

Why do use typecasting in java?

Type casting is used to convert from one datatype to the other


Is Javascript better than other scripting languages for web?

This is a very complicated issue but javascript was created to look like java but to be easier to use by nonprogrammers. This is probably one of the most desired traits of the javascript over other languages. javascript doesnot provide typecasting whereas java provides typecasting mechanism.


Why do you use typecasting?

to pretend our assumption correct.. i,e.. if we gave integers at the beginning of the program and then if we want to get our result in float , we us the method called typecasting by keeping float in braces in the condition or writing integer as float , means 5 as 5.0.


What has the author Paul Hayden Duensing written?

Paul Hayden Duensing has written: 'Stereo-types' 'Some types of note' 'Fourth biennial American Typecasting Fellowship conference' -- subject(s): American Typecasting Fellowship 'On type'


Is Dustin Hoffman is one example of an actor who has been caught in the typecasting trap?

false


When the director deliberately puts someone who does not appear to be right for the part in a role is known as?

typecasting


What is the pi conversion?

Pi has over two billion digits but you can use 3.14 or if you need a fraction 22/7


How do you use conversion factor in a sentence?

"I had to use a conversion factor to get the right unit for my answer on my chemistry quiz."


What conversion to use for conversion of 2.40 cm to inches?

Use the conversion1 cm = 0.3937 inch


What invention in 1456 made newspaper production much easier?

Johannes Gutenberg's invention. He was the first in the West to print using movable type. He introduced typecasting using a matrix, and was the first to use a press.


How To change from one SI prefix to another we use .?

To change from one SI prefix to another, we use the conversion factor of 10 raised to the power of the difference between the two prefixes. For example, to convert from centimeters (10^-2) to meters (10^0), we use a conversion factor of 10^2. Simply multiply the value by this conversion factor to make the conversion.


Is 12 inches over 1 foot a rate?

It is a conversion factor but, it could be considered a rate [of conversion].