Character pointers and integer pointers (++)

I have two pointers,

char *str1;
int *str2;

If I look at the size of both the pointers let’s assume

str1=4 bytes
str2=4 bytes

str1++ will increment by 1 byte, but if str2++ it will increment 4 bytes.

What is the concept behind this?


Simple, in the provided scenario:

  • char is 1 byte long
  • int (in your platform) is 4 bytes long

The ++ operator increments the pointer by the size of the pointed type.


When doing arithmetic on a pointer, it's always in terms of the objects pointed at, not in bytes.

So a pointer whose target object is e.g. four bytes, will increase it's actual numerical value by four when you add one.

This is much more usable, and makes far more sense than having all pointer arithmetic be in bytes.


A char is 1 byte, an int is (typically) 4 bytes. When you increment a pointer, you increment by the size of the data being pointed to. So, when you increment a char*, you increment by 1 byte, but when you increment an int*, you increment 4 bytes.


Pointer actualy holds the address of the memory location, which is 4bytes integer. str1 points to a location that holds 1byte, so if you increase the address of the str1 it jumps to next address of 1byte data. But in other case, str2 points to a 4byte data, so if you increase that address, it must jump over that data to get to the next 4byte data, so it incremets by 4.

This is how 1 byte sequence of data is stored in memmory:

ADDRESS:         FF334400  FF334401  FF334402  FF334403
DATA (1BYTE):           1         2         3         4

So if str1 wants to point to the number 2, it must hold its address, which is FF334401. If you increase str1, it must jump over the 2s address and get to 3, and to do that it must be incremented by 1.

In other case:

ADDRESS:         FF334400  FF334401  FF334402  FF334403 FF334404 ... FF334407
DATA (4BYTE):           0         0         0         1        0            2

Now if str2 points to the number 1 which is integer, and it actualy is 4byte data, it points to the begining of that data, which is address FF334400. When you increase it, it must jump over all 4 bytes of the 1s data to get to the 2s data, so it increases by 4 and its address becomes FF334404 which is the first byte of the 4byte data of the number 2.