Take the 2-minute tour ×

What does this mean: that a pointer increment points to the address of the next base type of the pointer?
For example:

p1++;  // p1 is a pointer to an int

Does this statement mean that the address pointed to by p1 should change to the address of the next int or it should just be incremented by 2 (I'm an int is 2 bytes), in which case the particular address may not contain an int?
I mean, if p1 is, say, 0x442012, will p1++ be 0x442014 (which may be part of the address of a double) or will it point to the next int which is in an address like 0x44201F?


share|improve this question

4 Answers 4

up vote 6 down vote accepted

Pointer arithmetic doesn’t care about the content – or validity – of the pointee. It will simply increment the pointer address using the following formula:

new_value = reinterpret_cast<char*>(p) + sizeof(*p);

(Assuming a pointer to non-const – otherwise the cast wouldn’t work.)

That is, it will increment the pointer by an amount of sizeof(*p) bytes, regardless of things like pointee value and memory alignment.

share|improve this answer

The compiler will add sizeof(int) (usually 4) to the numeric value of the pointer. If p1 is 0x442012 before the increment, then after the increment it will be 0x442012 + 4 = 0x442016.

Mind you, 0x442012 is not a multiple of 4, so it is unlikely to be the address of a valid four-byte int, though it would be fine for your two-byte ints.

It certainly won't go looking for the next integer. That would require magic.

share|improve this answer

p1++ just means p = p + sizeof (object pointed to by p).

Typically an int is 4 bytes, so it would increment by 4, but it depends on the sizeof() on your machine.

It does not go to "the next int".

share|improve this answer
p++ means p += 1 –  Karoly Horvath Jun 27 '11 at 11:57
No. p1++ means p1 += 1, nothing else. –  Konrad Rudolph Jun 27 '11 at 11:58
@Konrad: Well, unless you overload the ++ operator. Ah, the joys are redefining the meaning of operators (and a great way to make enemies) ;-) –  Skizz Jun 27 '11 at 12:01
@Skizz Even that won’t change the meaning since you cannot change the meaning of operators, so on a pointer, ++ will always have the same effect. –  Konrad Rudolph Jun 27 '11 at 12:04
@Konrad: class p { public: void operator++(int){exit(0);}};int main (){p a; a++; std::cout << "End" << std::endl;}; There, a++ now quits the program. I've seen programmers overload the ^ operator on a vector3d class to implement cross products! I know, it's a crime against programming. –  Skizz Jun 27 '11 at 12:14

Pointer arithmetic are done in sizoeof(*pointer) multiples - that is, for a pointer to int, increment will advance to the next integer (or 4 bytes for 32 bit integers).

share|improve this answer
"4 bytes for a 32 bit integer", well, that's not always true is it. Better say "(32 / CHAR_BIT) bytes in a 32 bit integer" (using the C++ definition of byte of course). But then again, who says integers are 32 bit? –  Skizz Jun 27 '11 at 12:07
There are hardly any architectures with a byte size different than 8 bits anymore. The int type is not necessarily 32 bits, but a "32 bit integer" is. I don't see you point... –  Eli Iser Jun 27 '11 at 12:13
There are numerous micro-controllers and DSPs that have weird configurations (there's a TI chip where all types are 16 bit). –  Skizz Jun 27 '11 at 12:17
@Skizz, I didn't said they were no architectures with a byte size different than 8 - I said there are hardly any. If you look into most (and perhaps all) of the (modern) CPUs and MCUs you'd see 8-bit bytes. DSPs are indeed wierd creatures. Some are 24 or 32 bits per byte. –  Eli Iser Jun 27 '11 at 12:19

Your Answer


