What data type is used to hold the memory address stored in the pointer variable? |
Pointers, like ints and chars, are
integer values.
However, C and C++ has a strict type system where the
treatment of these integer values differs based on the type.
This is because the hardware itself understands the type of thing to be integral to its proper value, and there is not necessarily a 1:1 correlation between a pointer value and the counting numbers.
For example, on some hardware, the nullptr is
not a zero value. C and C++ let you think it is, and treat it as such, but this is an abstraction to make handling explicitly invalid pointer values easy.
Likewise, on some (ancient) hardware, an int* type is
different than a char* type in actual size and bit patterns.
The popularity of the x86 family of processors has made a lot of people lazy about pointer aliasing, to the point where a lot of MS code even treats DWORD as an acceptable opaque type for internal OS pointer values.
The
actual structure of a pointer is implementation dependent, and this issue exists even today: a pointer to a class function is not the same as a standard void*, and you cannot safely perform type-punning with them.
tl;dr: Always use the proper type to manage pointers, and don’t play around with type-punning.