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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With