Declaring type of pointers?

Type-safety. If you don’t know what p is supposed to point to, then there’d be nothing to prevent category errors like

*p = "Nonsense";
int i = *p;

Static type checking is a very powerful tool for preventing all kinds of errors like that.

C and C++ also support pointer arithmetic, which only works if the size of the target type is known.

address occupies same amount of memory whatever my be the type

That’s true for today’s popular platforms. But there have been platforms for which that wasn’t the case. For example, a pointer to a multi-byte word could be smaller than a pointer to a single byte, since it doesn’t need to represent the byte’s offset within the word.

Leave a Comment