Ven Varu Ven Varu - 2 months ago 8
C Question

How does this hexadecimal arithmetic work?

I'm learning C pointers. I have incremented a double pointer by 1, as follows:

before ->

ptr_double =0x0128


then I incremented it by 1, and then the address stored in ptr_double increases by 8 bytes, that is
0x0128
+ 8 which gives
0x0130
.

I'm unable to understand how arithmetically
0x0130
comes.

I know this is probably a basic question, but I'm having difficulty understanding this. Can someone please explain this to me?

Answer

I assume you have a pointer to double like double* ptr_double;, which has its value 0x128, then you increment it ++ptr_double; (which makes the pointer jump with a sizeof(double), which in this case is 8). The address is in hexadecimal (base 16), so

0x128 + 0x8 = 0x130

Remember that hexadecimals belong to the range 0,1,...,9,A,B,C,D,E,F, so 0x8 + 0x8 = 10.