When subtracting two pointers in C

后端 未结 5 1977
星月不相逢
星月不相逢 2021-01-07 12:18

I was playing with pointers in order to fully get the concept and then wanted to subtract two pointers expecting the distance between these two addresses or something, but a

5条回答
  •  耶瑟儿~
    2021-01-07 12:38

    Pointer arithmetic works like that. It doesn't give you differences between two addresses. Instead it will show difference between two variables as if they are stored in an array. so, no matter if your variables (of same type ) are 4 bytes, 8 bytes or 1 byte, if stored in adjacent memory location their pointer subtraction will always result in 1 or -1.

提交回复
热议问题