-2

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.

  • 4
    _"when pointers only deal with the addresses"_ How did you conclude that pointers only deal with addresses? Your errors seem to imply that types are important as well. – Drew Dormann May 04 '22 at 12:35
  • 3
    Maybe a different example: `char a`, `int b` and `int* p`. If you try to do `p = &a;` and then `std::cout << *p;`, what happens? Compiler knows that `p` points to an `int`, so it reads 4 bytes instead of 1. – Yksisarvinen May 04 '22 at 12:36
  • @Yksisarvinen But, the pointer variable is storing the address of another data variable. So in the question, I mentioned the output of the address of both the *int* and *float* variables. I can't see any difference between those except location. – Vasukipriya K N May 04 '22 at 12:43
  • The difference is the type of the variable. Yes, on the assembly level `float*` and `int*` and `char*` are exactly the same thing (and maybe even the same as `int` or `float`), but the abstraction layer called "programming language" provides type system in order to prevent dumb type errors like the one in my example. – Yksisarvinen May 04 '22 at 12:52
  • @Yksisarvinen Yeah, that might be possible. Thank you. – Vasukipriya K N May 04 '22 at 12:54
  • Do not tag both C and C++ except when asking about differences or interactions between the two languages. The answers to this question are different for C and C++. Pick one language and delete the other tag. – Eric Postpischil May 04 '22 at 15:21

2 Answers2

3

When you dereference a pointer, the data type it points to must be known of the compiler so that it correctly interprets the data it finds there. This is why pointers are "typed".

As changing the "typeness" is a priori a mistake, this is not allowed except with an explicit cast.

  • So the problems occur while dereferencing? Am I right? Because the compiler doesn't know how many memory bytes to read. – Vasukipriya K N May 04 '22 at 12:40
  • 1
    @VasukipriyaKN: if you never dereferenced the pointers, there would be no problem. Except that the pointers would be of no use ;-) But the compiler does know how many bytes to read for every type ! –  May 04 '22 at 12:42
  • @YvesDaoust: “if you never dereferenced the pointers, there would be no problem”: This is not generally correct. If the pointer is not correctly aligned for the destination type, the behavior is not defined by the C standard. If the conversion is between a pointer to an object type and a pointer to a function type, in either direction, the behavior is not defined by the C standard. – Eric Postpischil May 04 '22 at 12:48
  • Re “As changing the "typeness" is a priori a mistake, this is not allowed except with an explicit cast”: Some implicit conversions that change the type are allowed. Implicitly converting between a pointer to any object type and a pointer to `void` is allowed. Implicitly converting between compatible types is allowed. Implicitly adding qualifiers is allowed. – Eric Postpischil May 04 '22 at 12:50
  • @EricPostpischil: I was speaking of conversions between pointers, and changing "typeness", not attributes. The only exception is the assignment of a pointer to a void*. –  May 04 '22 at 13:24
  • @YvesDaoust: There are more exceptions than just to `void *`. There is also from `void *` and to and from pointers to compatible types. Compatible types include arrays with known and unknown sizes, so an `int (*)[3]` may be assigned to an `int (*)[]` and vice-versa. They also include functions with and without prototypes or functions with prototypes whose parameters have compatible types. And they include enumerations with implementation-defined integer types. – Eric Postpischil May 04 '22 at 14:08
  • @EricPostpischil: `error C2440: '=' : cannot convert from 'void *' to 'int *'`. What is the point of this rearguard action ? –  May 04 '22 at 14:21
  • @YvesDaoust: That is a C++ error message. I was speaking about C, having failed to notice the OP improperly double-tagged the question. – Eric Postpischil May 04 '22 at 15:20
1

The datatype only helps in allocating the required memory size to the specified datatype.

This is not true. The type of a pointer p also tells the compiler what type to use for the expression *p.

If p is an int *, then *p has type int, and, if the program uses an expression such as a *p + 3, the compiler will generate an integer add instruction (or equivalent code). If p is a float *, then *p has type float, and, if the program uses an expression such as *p + 3, the compiler will generate a floating-point add instruction (or equivalent code). These different instructions will cause the computer to treat the bits of *p differently.

When it comes to the address of the memory, irrespective of the datatype of the variable, both addresses will be in the same format.

This is often true in C implementations but is not always true. The C standard allows pointers of different types to have different representations, with certain exceptions. (Pointers to character types and to void must have the same representation as each other. Pointers to structure types must have the same representation as each other. Pointers to union types must have the same representation as each other.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312