Comment

drew

"casting" usually means using an object of one type as another. In C, (char *)voidPtr doesn't convert anything, and it doesn't make a new object. It just uses voidPtr as a char*. I recommend saying 'convert' (like you did most of the time) and leaving the term 'cast' for times when an object is forced to be interpreted as something else (which isn't happening in your demos).

Replies

Peter Bengtsson

Thanks! Interesting. Cast is like when a man dresses up like a drag queen. Doesn't make him a woman.
Converting is like a man having a sex change operation then I guess.
:)

Robert

What a fantastic analogy! Thanks!

mypalmike

Casting does mean converting. In both C and Python, casting from float to int is very much a conversion. It takes the original fp number, which is generally represented internally as an IEEE 754 floating point value, and converts it to an twos completment integer representing the floor of the value. The new value has a completely different representation than the original. In other words, it is a different object.

Your example of casting pointers doesn't modify anything because a pointer is a pointer, so no underlying representation needs to be changed for the semantics of conversion to take place.

FutureNerd

Casting in C (or C++ or Java) is that (int) x syntax. It sometimes means converting, sometimes means reinterpreting the bytes, sometimes both! E.g., in C:
<pre>
char x = -1;
long y = (unsigned char) x; // y = 255
long z = (unsigned short) x; // z = 65535 (!)</pre>

The Python int(x) syntax more clearly implies "convert" to my eyes.