Add an offset to a Byte Ptr
BlitzMax Forums/BlitzMax Programming/Add an offset to a Byte Ptr
| ||
If a function accepts a Byte Ptr as a parameter, is this a correct way to indicate an offset?: somefunction bank.buf()+4 |
| ||
Yep. That should work. |
| ||
Buf() returns a Byte Pointer so yes that's correct. If you had a type and wanted the memory address of that, you'd use varptr(mytype) |
| ||
Please note that if you add 4 to a byte pointer you are adding 4 BYTES to the address, whereas for example adding 4 to an Int pointer actually adds 16 bytes, and short pointers would add 8 bytes. |
| ||
Cripes I never knew that, I would have assumed it ALWAYS added bytes only. Fair enough, it's because I used to use Bytes when it was "normal" in computers of the olden day. |
| ||
Yah any value based math that you do with a pointer of a particular type, the values are converted to the `sizeof` the type, so: BytePtr+1 = Byte Pointer Address + 1 ShortPtr+1 = Short Pointer Address + 2 IntPtr+1 = Int Pointer Address + 4 IntPtr + Offset = Int Pointer + (Offset * 4) This is why when you design your algorithm which uses pointers, e.g. to move data around or work with pixels in a pixmap, you have to remember that the amount that you increment the pointer by depends on its type, so you can skip through RGBA pixels by adding 1 to an Int Pointer, but if you're working off a Byte Pointer you have to add 4. |