Pointer addition and element size

后端 未结 3 1270
无人共我
无人共我 2020-12-16 17:07

At: http://www.fredosaurus.com/notes-cpp/arrayptr/26arraysaspointers.html

Under: Pointer addition and element size

There is the following co

3条回答
  •  执念已碎
    2020-12-16 17:52

    (I assume that you mean "1" in the last line, not "p")

    Pointer arithmetic in both C and C++ is a logical addition, not a numeric addition. Adding one to a pointer means "produce a pointer to the object that comes in memory right after this one," which means that the compiler automatically scales up whatever you're incrementing the pointer with by the size of the object being pointed at. This prevents you from having a pointer into the middle of an object, or a misaligned pointer, or both.

提交回复
热议问题