Pointer Arithmetic [closed]

Here is where I learned pointers: http://www.cplusplus.com/doc/tutorial/pointers.html

Once you understand pointers, pointer arithmetic is easy. The only difference between it and regular arithmetic is that the number you are adding to the pointer will be multiplied by the size of the type that the pointer is pointing to. For example, if you have a pointer to an int and an int's size is 4 bytes, (pointer_to_int + 4) will evaluate to a memory address 16 bytes (4 ints) ahead.

So when you write

(a_pointer + a_number)

in pointer arithmetic, what's really happening is

(a_pointer + (a_number * sizeof(*a_pointer)))

in regular arithmetic.


First, the binky video may help. It's a nice video about pointers. For arithmetic, here is an example:

int * pa = NULL;
int * pb = NULL;
pa += 1; // pa++. behind the scenes, add sizeof(int) bytes
assert((pa - pb) == 1);

print_out(pa); // possibly outputs 0x4
print_out(pb); // possibly outputs 0x0 (if NULL is actually bit-wise 0x0)

(Note that incrementing a pointer that contains a null pointer value strictly is undefined behavior. We used NULL because we were only interested in the value of the pointer. Normally, only use increment/decrement when pointing to elements of an array).

The following shows two important concepts

  • addition/subtraction of a integer to a pointer means move the pointer forward / backward by N elements. So if an int is 4 bytes big, pa could contain 0x4 on our platform after having incremented by 1.
  • subtraction of a pointer by another pointer means getting their distance, measured by elements. So subtracting pb from pa will yield 1, since they have one element distance.

On a practical example. Suppose you write a function and people provide you with an start and end pointer (very common thing in C++):

void mutate_them(int *begin, int *end) {
    // get the amount of elements
    ptrdiff_t n = end - begin;
    // allocate space for n elements to do something...
    // then iterate. increment begin until it hits end
    while(begin != end) {
        // do something
        begin++;
    }
}

ptrdiff_t is what is the type of (end - begin). It may be a synonym for "int" for some compiler, but may be another type for another one. One cannot know, so one chooses the generic typedef ptrdiff_t.


applying NLP, call it address arithmetic. 'pointers' are feared and misunderstood mostly because they are taught by the wrong people and/or at the wrong stage with wrong examples in the wrong way. It is no wonder that nobody 'gets' it.

when teaching pointers, the faculty goes on about "p is a pointer to a, the value of p is the address of a" and so on. it just wont work. here is the raw material for you to build with. practice with it and your students will get it.

'int a', a is an integer, it stores integer type values. 'int* p', p is an 'int star', it stores 'int star' type values.

'a' is how you get the 'what' integer stored in a (try not to use 'value of a') '&a' is how you get the 'where' a itself is stored (try to say 'address')

'b = a' for this to work, both sides must be of the same type. if a is int, b must be capable of storing an int. (so ______ b, the blank is filled with 'int')

'p = &a' for this to work, both sides must be of the same type. if a is an integer, &a is an address, p must be capable of storing addresses of integers. (so ______ p, the blank is filled with 'int *')

now write int *p differently to bring out the type information:

int* | p

what is 'p'? ans: it is 'int *'. so 'p' is an address of an integer.

int | *p

what is '*p'? ans: it is an 'int'. so '*p' is an integer.

now on to the address arithmetic:

int a; a=1; a=a+1;

what are we doing in 'a=a+1'? think of it as 'next'. Because a is a number, this is like saying 'next number'. Since a holds 1, saying 'next' will make it 2.

// fallacious example. you have been warned!!! int *p int a; p = &a; p=p+1;

what are we doing in 'p=p+1'? it is still saying 'next'. This time, p is not a number but an address. So what we are saying is 'next address'. Next address depends on the data type, more specifically on the size of the data type.

printf("%d %d %d", sizeof(char), sizeof(int), sizeof(float));

so 'next' for an address will move forward sizeof(data type).

this has worked for me and all of the people I used to teach.


I consider a good example of pointer arithmetic the following string length function:

int length(char *s)
{
   char *str = s;
   while(*str++);
   return str - s;
}