If we have to hold an address of any data type then we require a pointer of that data type.
But a pointer is simply an address, and an address is always int
Because your assumption that "address is always int type" is wrong.
It's totally possible to create a computer architecture where, for instance, pointers to characters are larger than pointers to words, for some reason. C will handle this.
Also, of course, pointers can be dereferenced and when you do that the compiler needs to know the type of data you expect to find at the address in question. Otherwise it can't generate the proper instructions to deal with that data.
Consider:
char *x = malloc(sizeof *x);
*x = 0;
double *y = malloc(sizeof *y);
*y = 0;
These two snippets will write totally different amounts of memory (or blow up if the allocations fail, nevermind that for now), but the actual literal constant (0
which is of type int
) is the same in both cases. Information about the types of the pointers allows the compiler to generate the proper code.
Pointers are not just int
. They implicitly have semantics.
Here are a couple of examples:
p->member
only makes sense if you know what type p
points to.
p = p+1;
behaves differently depending on the size of the object you point to (in the sense that 'p' in in fact incremented, when seen as an unsigned integer, by the size of the type it points to).
You can have a typeless pointer in C very easily -- you just use void *
for all pointers. This would be rather foolish though for two reasons I can think of.
First, by specifying the data that is pointed to in the type, the compiler saves you from many silly mistakes, typo or otherwise. If instead you deprive the compiler of this information you are bound to spend a LOT of time debugging things that should never have been an issue.
In addition, you've probably used "pointer arithmetic". For example, int *pInt = &someInt; pInt++;
-- that advances the pointer to the next integer in memory; this works regardless of the type, and advances to the proper address, but it can only work if the compiler knows the size of what is being pointed to.
It's mostly for those who read the code after you so they could know what is stored at that address. Also, if you do any pointer arithmetics in your code, the compiler needs to know how much is he supposed to move forward if you do something like pSomething++, which is given by the type of the pointer, since the size of your data type is known before compilation.
Because the type of a pointer tells the compiler that at a time on how many bytes you can perform the operation.
Example: in case of char
, only one byte. And it may be different in case of int of two bytes.
There are several reasons:
char
is different from reading or writing a double
.Note that there is a pointer type that means "simply a pointer" in C, called void*
. You can use this pointer to transfer an address in memory, but you need to cast it to something useful in order to perform operations in the memory pointed to by void*
.