C++ Pointer addition being multiplied -
this question has answer here:
- c++ pointer arithmetic weirdness 5 answers
i working on packet code, , need move pointer x bytes. when tell pointer pointer = pointer + x; proceed print pointer via to_string or cout, x*2 has been added pointer. have tried multiple values, both constant , variable. have verified pointer's value before operation. thing can think when happens 3 being bit-shifted somewhere. ideas?
sample code:
cout << "before: " << pointer << "\n"; pointer = pointer + 3; cout << "after: " << pointer << "\n"; output:
before: 0x7ffff4604e93 after: 0x7ffff4604e99 my includes:
#include <cstring> #include <cstdlib> #include <cstdarg> thank you,
mike
pointer = pointer + x; not move pointer x bytes, moves x * sizeof(*pointer) bytes. in case, if size of struct 2 bytes, pointer = pointer + 3 should move 6 bytes, has.
Comments
Post a Comment