Reputation: 19502
As far as I know about data types in C/C++, while declaring a variable, we need to declare its data type, which tells the compiler to reserve the number of bytes in the memory accordingly.
But in the case of pointers, we know that their size is constant (eg. 2 bytes in "Turbo Compiler"), irrespective of the data type of the variable it is pointing to, because the pointer is storing a memory address as an unsigned integer.
My question is, if the pointers are always a constant size in bytes, then what is the need of mentioning the data type while declaring them? Is my understanding about pointers wrong?
Upvotes: 49
Views: 19121
Reputation: 2788
Data type of a pointer is needed in two situations:
How it is used in dereferencing the pointer?
Consider the following example:
{
char *k; //poniter of type char
short j=256;
k=&j; // Obviously You have to ignore the warnings
printf("%d",*k)
}
Now because k is of type char
so it will only read one byte. Now binary value of 256
is 0000000100000000
but because k is of type char
so it will read only the first byte hence the output will be 0.
Note: if we assign j=127 then the output will be 127 because 127 will be held by the first byte.
Now come to pointer arithmetic:
Consider the following example:
{
short *ptr;
short k=0;
ptr=&k;
k++;
ptr++;// pointer arithmetic
}
Are statements k++
and ptr++
are same thing? No, k++
means k=k+1
and ptr++
means ptr=ptr+2
. Because the compiler "knows" this is a pointer and that it points to a short, it adds 2 to ptr instead of 1, so the pointer "points to" the next integer.
For more info refer second chapter of this tutorial.
Upvotes: 22
Reputation: 1467
The data type is needed when dereferencing the pointer so it knows how much data it should read. For example dereferencing a char
pointer should read the next byte from the address it is pointing to while an int
pointer should read 2 bytes.
Upvotes: 74
Reputation: 5605
The problem is not about pointer size but pointer dereferencing. (wether in C or C++)
Say you have:
int* someint;
float* somefloat;
*someint
references a memory size of sizeof(int)
, whereas *somefloat
references a memory size of sizeof(float)
which are different.
Upvotes: 5
Reputation: 67829
Assume that this code compiles without error (as you would like):
int a;
int b = 42;
void * d = &b;
a = *d;
What should be the value of a?
Now with this one:
int a;
float b = 42.0;
void * d = &b;
a = *d;
What do you expect in a?
Actually the type specifies how should the pointed area be interpreted. You should specify int *
in the first example and float *
in the second one, instead of void *
.
Upvotes: 3
Reputation: 7380
it is the concept of a strong typing used in c++. the size of the pointer may be the same but the size of the pointed type may differ. you can always cast a pointer of one type into a pointer of another type however.
Upvotes: 0
Reputation: 9740
What size a pointer needs depends on the system you are using. On a x86_64
system the pointer size might by 64 bit.
The reason why you need the data type for pointers is because the compiler has to know what the size of the memory cell is, among others, the pointer is pointing to. Also type safety cannot be ensured w/o the type. Also, you would have to typecast every pointer when accessing structures from the pointer.
You also could use a void
pointer and do everything by hand. But why should you want that?
Upvotes: 2
Reputation: 182639
First of all the size and representation of the pointers themselves aren't always the same for different types. It's just something that happens on many implementations.
Second, when using pointers you don't care about the size of the pointers themselves. You need the size of the pointed type.
For example, try this:
int var[5];
char *c = (char *)var;
int *x = var;
printf("%p\n%p\n", p + 1, x + 1);
You'll see pointer arithmetic strongly depends on the size of the pointed type.
Upvotes: 7