Not all pointers are (required to be of) the same size. If you had a large structure that needs to be aligned to 10 MB, the compiler could decide that it only needs 8 bits (instead of the usual 32 or 64) to store all possible addresses your variable could be located at.
You also don't need a different datatype, you can use void*
just fine, but why would you? In C++, this is a code-smell.
There's also type-safety. You know an int*
points to an int
, and this is to your advantage.
Two reasons:
On some architectures, pointers may have different formats depending on the size of the data they're pointing to. For instance, a pointer to char
must be able to address individual bytes, but a pointer to int
only needs to be able to address groups of 4 bytes. So the latter could use a format that contains the byte address divided by 4.
It allows the compiler to cgenerate correct programs. If you try to dereference a char
pointer and assign it to a int
, it needs to know that it should only read one byte from the source, and widen it to the size of an int
. Without the pointer type declaration, it would read more bytes than appropriate.
For starters, I don't know of a machine where pointers are stored as hexadecimal; every machine I'm familiar with used a binary representation internally. (For the last 30 or 40 years, at least. IIRC, the IBM 1401 used decimal everywhere.)
As others have pointed out, not all pointers do have the same
size and representation. I've worked on machines where char*
was bigger than other data pointer types, and of course, having
different sizes for function pointers and data pointers used to
be very common.
The real answer, however, it based on the type system in C and
C++. If p
is a pointer, what is the type of *p
? If I write
something like *p + *q
, the compiler has to know whether to use
integer arithmetic or floating point arithmetic, for example.
As for your second question: typically, yes, although you'll
probably need a reinterpret_cast
in there somewhere. However,
the only thing you can legally do with your int*
is cast it
back to a float*
; dereferencing it is undefined behavior.
There are some exceptions for char*
and unsigned char*
. And
in practice, if you know what you are doing, you can get away
with things like:
float f;
int* a = reinterpret_cast<int*>( &f );
std::cout << *a << std::endl;
I actually do similar things for some sorts of low level
debugging or programming; things like extracting the exponent
field from a float
. Such code is extremely rare, however, and
formally involves undefined behavior, so you have to verify what
the compiler actually does, and possibly turn off certain
optimizations.
Because it gives information on how to interpret data pointed by the pointer.
EX:int *a; Can we use this a for storing float address.
Through C type unsafety feature: yes, but not directly, especially in recent compilers and standard (which tend to be safer and safer)
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