Reputation: 967
What I know about pointer is, it is used to point to specific location (memory address), so why do we even need the same data type of pointer as that of the variable we are trying to point. Suppose I create a variable of integer, then I have to create a pointer to integer to point it. So why can't I create a void pointer or float pointer to point the value stored in that integer variable! Am I missing some concepts of pointers?
Upvotes: 1
Views: 5985
Reputation: 123548
Pointer arithmetic is the main reason - if p
points to an object of type T
, then p+1
points to the next object of that type. If p
points to a 4-byte int
, then p+1
points to the following 4-byte int
. If p
points to a 128-byte struct
, then p+1
points to the following 128-byte struct. If p
points to a 2 Kbyte array of double
, then p+1
points to the next 2 Kbyte array of double
, etc.
But it's also for the same reason we have different types in the first place - at the abstract machine level, we want to distinguish different types of data and operations that are allowed on that data. A pointer to an int
is different from a pointer to a double
because an int
is different from a double
.
Upvotes: 1
Reputation: 518
A small addition on Max Langhof's answer:
It is important to realise that in the end, variables are stored simply as a sequence of bits (binary digits), e.g. 01010101 00011101 11100010 11110000. How does your program know what this 'means'? It could be an integer (which is often 4 bytes on modern architectures), it could be a floating-point value. For the memory involved this makes no difference, but for your code the implications can be huge. Therefore, if you refer to this memory location (using a pointer), you will need to specify how the bytes there should be converted to decimal (or other) values.
Upvotes: 2
Reputation: 12404
Memory accesses do not work without knowing what kind of data object you are dealing with.
Imagine some simple assignment:
int a, b=10;
float f;
a = b; // same type => Just copy the integer
f = b; // wrong type => Convert to float.
This works fine because the compiler knows that both variables are of a certain type and size and representation. If the types do not match, a proper conversion is applied.
Now the same with typed pointers:
int a = 10;
float f;
int *pa;
float *pf;
f = a; // Type conversion to float applied
*pa = a; // Just copy
*pf = a; // Type conversion
If you take away the knowledge about the memory location where the pointer points to, how would the compiler know if a conversion is required? Or if some integer propagation is needed or is an integer is truncated into a shorter type?
More problems are waiting around the corner if you want to use a pointer to address elements of an array. Pointer arithmetics won't fly without types.
Types are essential. For variables as well as for pointers.
Upvotes: 0
Reputation: 860
You are right. Although int
and float
are different types, there should be no difference between their pointers int*
and float*
. In general, this is the case. However, the binary representation is different between int
and float
. Therefore accessing an int
with a float*
pointer leads to garbage being read from the RAM.
Furthermore, what you have on your machine is not the general case, but hardware and implementation dependent.
For example: float
and int
variables are usually 32bit long. However, there are systems where the int
has only 16bit. What happens now if you try to read a float
from a int*
pointer? (Or, even if both are 32bit, what happens if you try to read a float
from a char*
?)
Upvotes: 0
Reputation: 25
The pointer type is also important when dealing with arrays, as arrays and pointers are interchangeable. When you increment a pointer (which is what indexing does), you need to know how big the type is (int, long, structure, class) in order to access the next item.
arr[5] == *(arr+5) but 5 what? This is determined by the type.
Upvotes: 3
Reputation: 23701
So why can't I create a void pointer [...] to point the value stored in that integer variable
You can do that, no problem:
int x = 10;
double y = 0.4;
void* v = &x;
v = &y;
But now imagine a function like this:
void print(void* value)
How would this function know what to do with the memory at the pointer location? Is it an integer? Or a floating point number? A float
or a double
? Maybe it's a huge struct
or an array of values? You must know this to dereference the pointer (i.e. read the memory) correctly, so it only makes sense to have different pointer types for pointers to different types:
void print(int* value)
This function knows that the pointer points to an int
, so it can happily dereference it to get an int
value.
Upvotes: 5