Both addition and subtraction have a different behavior with pointers according to the size of the data type to which they point.
For example, let's assume that in a given compiler for a specific machine, char
takes 1 byte, short
takes 2 bytes and long
takes 4.
Suppose we define the following variables:
char *pChar;
short *pShort;
long *pLong;
and that we know that they point to memory locations 1000, 2000 and 3000 respectively, so if we write:
pChar++; // As expected, pChar will contain the value 1001
pShort++; // pShort will contain the value 2002, because short type will take 2 bytes
pLong++; // pLong will contain the value 3004
Back to code in the question, according to the pointer arithmetic, the variable size will contain the size of i
(in this case int
) regardless the cast to (char *) or (int *)