Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer Arithmetic [closed]

Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure the audience is a bunch of Java programmers learning C and C++.

like image 878
leora Avatar asked Dec 27 '08 07:12

leora


People also ask

What is a pointer arithmetic?

Address arithmetic is a method of calculating the address of an object with the help of arithmetic operations on pointers and use of pointers in comparison operations. Address arithmetic is also called pointer arithmetic.

What is pointer arithmetic with example?

When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. For Example: If an integer pointer that stores address 1000 is incremented, then it will increment by 2(size of an int) and the new address it will points to 1002.

Which arithmetic operations are allowed on pointers?

You can perform a limited number of arithmetic operations on pointers. These operations are: Increment and decrement. Addition and subtraction.

Why is pointer arithmetic not applicable on void?

Since void is an incomplete type, it is not an object type. Therefore it is not a valid operand to an addition operation. Therefore you cannot perform pointer arithmetic on a void pointer.


2 Answers

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.

like image 60
Paige Ruten Avatar answered Sep 23 '22 19:09

Paige Ruten


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.

like image 29
Johannes Schaub - litb Avatar answered Sep 20 '22 19:09

Johannes Schaub - litb