What is the difference between float pointer and int pointer address?

后端 未结 3 1410
醉话见心
醉话见心 2021-01-02 09:20

I tried to run this code,

int *p;
float q;
q = 6.6;
p = &q;

Though it will be a warning, but i think &q and p

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-02 09:32

    In addition to what is said by teppic,

    Consider,

    int a = 5;
    int *p = &a;
    

    In this case we indicate to the compiler that p is going to point to an integer. So it is known that when we do something like *p , at runtime, the no. of bytes equal to size of an int would be read.

    If you assign address of a datatype occupying x number of bytes to a pointer of which is declared to hold the address of datatypes of fewer bytes than x, you read the wrong number of bytes when using the indirection operator.

提交回复
热议问题