Sign in to follow this  
slaker

i dont remember exactly this thing

Recommended Posts

first of all sorry for any spelling erors i might have i saw once something in a c++ source file and i am curious what does it mean i don`t remember exactly the structure but it was something like (int)*var or (int *)var or (int *)&var i`ve searched this code but i didnt found what i was looking for my question is: what is the right form, what it is used for and what does it mean

Share this post


Link to post
Share on other sites
There is no right form. All of them could be OK, depending on what var is and on what you are trying to do.

(int)*var - take the value var is pointing to and cast to an int
(int *)var - take var and and cast it to a pointer to an int
(int *)&var - take the address of var and cast it to a pointer to an int

shmoove

Share this post


Link to post
Share on other sites
As shmoove pointed out, those are C-style casts. Casting simply means converting a variable from one type to another. The type to cast to is in parantheses, and the variable to cast follows the closing paranthesis.

The C++ equivalent of C-style casting is static_cast, whose syntax is as follows:

C-style:

char charVar = 'a';
int intVar = ( int )charVar;

C++-style:

char charVar = 'a';
int intVar = static_cast< int >( charVar );

There are a few other C++ casts as well. This is an excellent article covering them in detail.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this