In C++, is (int) ch equivalent to int(ch).
If not, what's the difference?
The & is a unary operator in C which returns the memory address of the passed operand. This is also known as address of operator. <> The * is a unary operator which returns the value of object pointed by a pointer variable.
&n writes the address of n . The address of a variable points to the value of that variable.
They are the same thing, and also the same as (int)(ch)
. In C++, it's generally preferred to use a named cast to clarify your intentions:
static_cast
to cast between primitive types of different sizes or signednesses, e.g. static_cast<char>(anInteger)
.dynamic_cast
to downcast a base class to a derived class (polymorphic types only), e.g. dynamic_cast<Derived *>(aBasePtr)
.reinterpret_cast
to cast between pointers of different types or between a pointer and an integer, e.g. reinterpret_cast<uintptr_t>(somePtr)
.const_cast
to remove the const
or volatile
qualifiers from variables (VERY DANGEROUS), e.g. const_cast<char *>(aConstantPointer)
.int(x)
is called function-style cast by the standard and is the same as the C-style cast in every regard (for POD) [5.2.3]:
If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With