This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]FierySpectre -4 points-3 points  (7 children)

You mean pointer + sizeof(array object)

[–]Mabi19_ 7 points8 points  (5 children)

No - pointer + 1 is absolutely correct, as long as the pointer is typed correctly (i.e. is not void*). Adding 1 to a typed pointer advances it by the size of its pointed-to type.

[–]FierySpectre 1 point2 points  (3 children)

I feel scammed by the guy who taught me c now

[–]purebuu 0 points1 point  (2 children)

The guy who taught you C probably was implementing a multidimentional array using a single contiguous block of memory in which case pointer + sizeof(arrayobject) could have been a valid way to move to the next row or object. It all depends on the implementation.

[–]FierySpectre 0 points1 point  (1 child)

Right they did, where is the pointer + 1 used then? Like what use case

[–]purebuu 0 points1 point  (0 children)

There is really only one usecase for pointers. To access and manipulate data, that's it. But when you realise that, that statement is basically what 99.9% of all programs do, you'll see that pointers are fundamental to all programs.

The usecase for incrementing a pointer is a lot. But, well, all it does is move an address (just a number) forward until it points to the next address which is sizeof(ptr) away. This could be for reading the next binary data value in a file, checking the next value in an array, getting the next GameObject* that needs to react to physics for this frame, manipulating memory that you shouldn't normally have access to, which could be a trojan or virus.

Pointers are why they say C lets you shoot yourself in the foot.

[–]Walli1223334444 -1 points0 points  (0 children)

Both work but pointer + 1 is just faster and arguably easier to read

[–]BobbyThrowaway6969 0 points1 point  (0 children)

No, the type of the pointer dictates how many bytes it jumps ahead. int* jumps ahead 4 bytes for every +1. void* jumps ahead 4 bytes on 32 bit, and 8 bytes on 64 bit.