Code snippet 1:
int main(){
float fl;
int *i=&fl;
}
The error was:
error: cannot convert 'float*' to 'int*' in initialization int *i=&fl;
Code snippet 2:
int main(){
int i;
float *fl=&i;
}
The error was:
error: cannot convert 'int*' to 'float*' in initialization float *fl=&i;
Question
The datatype only helps in allocating the required memory size to the specified datatype. When it comes to the address of the memory, irrespective of the datatype of the variable, both addresses will be in the same format.
For Example-
int a;
float b;
cout<<&a<<" "<<&b;
The output was_
0x61fe1c 0x61fe18
So by looking at the address, one can't differentiate between datatypes. So when the pointer deals with the addresses, why can't we assign an integer pointer to the float variable?
NOTE: Here I'm not talking about the size of the datatype or the number of bytes that each datatype takes or the data format that each instruction is stored. I'm only interested in the address of the variable.