What is pointer arithmetic?This tip submitted by Umer khan on 2010-10-27 21:56:42. It has been viewed 1982 times.
Rating of 6.2 with 15 votes
When you have a pointer such as int *p, and you write: *(p + 1) this is the same as writing p[ 1 ]. Writing p + 1 performs pointer arithmetic, adding not a single byte to the address stored in p, but sizeof( int ) bytes! If you need to work with individual bytes, you can use a typecast: (char *)p + 1 will advance you exactly a single byte. This can be useful when performing file IO.
If you are working with a pointer to a single value, most of the time you won't want to use pointer arithmetic. When you are working with an array, you can use it instead of array notation.
The most valuable thing you can learn from pointer arithmetic is that that pointers are just variables that happen hold memory addresses, and you can manipulate them like any other variables.
Help your fellow programmers! Add a tip!