The code you are showing is illegal (non portable C++).
The syntax (int *)
means that the address of the string literal "tiger"
is casted to a pointer to an integer but this is not allowed and it may result in undefined behavior.
You are allowed to cast any pointer type to a pointer to a char or to a pointer to unsigned char, but not the other way around.
The reason is that there are architectures in which integers and other large types MUST be at certain address (e.g. at an address that is a multiple of 4). Just creating the pointer with an invalid address is enough for undefined behavior (dereferencing is not necessary because on some CPUs even just loading an invalid address to a register used for indexing integers can raise an hardware trap).
On a PC (x86) the conversion doesn't creates problems (except may be performance problems) and simply means that the compiler will treat the memory containing "tige"
as if it was an integer. Dereferencing the integer (i.e. printing *((int *)&cat)
) should give 1701276020 that is
116 + // ASCII code for 't'
105 * 256 + // ASCII code for 'i'
103 * 65536 + // ASCII code for 'g'
101 * 16777216 // ASCII code for 'e'