Reason of pointer of same datatype

Is it compulsory to have the same data type of pointer to hold the adress ? what will happen if we do so?

Hey Aman, it is necessary that the data type of the pointer and the data type of the value that you are assigning to it must be same. Otherwise, it won’t work correctly. Because data type of pointer defines that how the pointer will access the memory. For eg. if pointer is of int data type then it will point to a 4 bytes of memory but if its of char data type then it will point to 1 byte of memory.

Hi Aman, for further clarification, consider this example.
For a pointer declared int *pi;, a statement like double x = *pi / 2 will truncate the division, while with a float *pf;, float x = *pf / 2 will give you a fractional result. So you surely need to tell the compiler about the pointer type, for some quite important behaviour depends on that info like there is the * operator for dereferencing a pointer, and the [] indexing operator for relative addressing, and both need to know the pointer type.