What does a C cast really do?

c, casting, types

Solution

A cast in C is only meaningful at compile time because it tells the compiler how you want to manipulate a piece of data. It does not change the actual value of the data. For example, `(int*)p` tells the compiler to treat `p` as a memory address to an integer. However this costs nothing at run time, the processor just deals with raw numbers the way they are given to it.

Problem

I write more and more C applications, and now I wonder something about casts. In C++, a dynamic cast is a very costly operation (for instance a down-cast), but I don’t even know for static one. In C, I had to write something like that: ``` assert ( p ); /* p is void* */ int v = *(int*)p; ``` Is it a « C dynamic-cast »? Is it quite the same as the `static_cast<int*>(p)` of C++? How much does it cost? Thanks in advance.

Original source

Related problems