- Basically pointer is a variable used to store memory address ,which is always
hexadecimal(memory address) then why do we need different datatype for storing address. - EX:int *a; Can we use this a for storing float address.

- 93
- 4
-
2"memory address, which is always hexadecimal" No. That is up to the representation, which is on another layer. – glglgl Oct 10 '13 at 09:42
-
While searching on web I got [Why different types of pointer fordifferent data type in c](http://stackoverflow.com/questions/12530806/why-different-types-of-pointer-for-different-data-type-in-c) – Grijesh Chauhan Oct 10 '13 at 10:24
-
well, the representation is never visible... – Karoly Horvath Oct 11 '13 at 11:12
5 Answers
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.

- 253,575
- 64
- 457
- 625
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 toint
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 aint
, it needs to know that it should only read one byte from the source, and widen it to the size of anint
. Without the pointer type declaration, it would read more bytes than appropriate.

- 741,623
- 53
- 500
- 612
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.

- 150,581
- 18
- 184
- 329
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)

- 9,192
- 4
- 24
- 38
why do we need different datatype for storing address
That's actually the correct question, and the answer is hidden in it - why do we need a different datatype for storing addresses. We (the programmers) need it. The machine doesn't care - one number is just like the other.
If you think of a "variable" as a "placeholder" for some data, there is a dichotomy in programming languages between using the data itself and using the variable. Sometimes you need just the data (e.g. you need to print it out) and sometimes you need the address where this data is stored (e.g. so you can modify it). Most languages have syntactic sugar which confuses these two cases, and treat the variable identifier differently in different contexts.
One such case goes like this: consider the statement
a = 1;
In this case, the compiler looks up the address of a variable identified by "a", and writes "1" to this address. The identifier "a" might as well be a pointer. Now look at a different thing:
if (a == 1) ... ;
You are not comparing the address of a variable identified by "a" with something, you are comparing what is stored at this address with "1".
The various "pointer types" exist again for programmer convenience: it's basically so we make fewer bugs by wrongly accessing data. Look at this example:
double d;
double* dp;
int i;
int* ip;
By default, C is very relaxed about it, and you can usually do a:
dp = ip;
or
dp = &i;
... but! sizeof(i)==4 and sizeof(d)==8, so if you dereference the dp pointer you would be trying to read 8 bytes from a variable (i) which holds only 4. This means you would be reading 4 unpredictable (random) bytes after the first four bytes of i, which is definitely something you don't want to do.
Again, all of this is for our convenience. The machine doesn't care. Both pointers look and behave exactly the same to the CPU.

- 1,895
- 1
- 13
- 20