SEARCH THIS BLOG

POINTER ARITHMETIC

In this article we will see which operators work with pointers and how they operate on them. The study of this topic is termed as Pointer Arithmetic.

 

POINTER ARITHMETIC

Before we start discussing about the arithmetic of operators, it is important that we understand how pointers store memory address. As we know, pointers are variables that hold memory address of other variables, any modification in the value of a pointer means changing the memory address to which it is pointing.

 

How does Computer Memory look like?

Visualize the computer memory as a table consisting of number of rows & columns depending on the memory capacities of the computer. Each of the boxes formed by the intersection of rows and columns has a size of 1 byte. Now each of these boxes have a memory address which are sequential (say 1,2,3, ……. and so on).

 

Pointer Arithmetic
A general representation of computer memory















Now, when a variable is created, a number of these sequential memory boxes merge to give rise to a new box of required size. For instance,

Suppose we create an integer variable.


int a;


As soon as the compiler reads this variable, it merges 4 sequential memory boxes to give rise to a new memory box of size 4 bytes (i.e. size of int variable). This new memory box is given the same address as its first constituting box. Suppose a is at address 117.

See the figure below to understand how the memory looks now :

 

Pointer Arithmetic
Variable created at Base address 117















Note that the address of variable is saved as 117 (i.e. the starting position box). The location of first box is termed as Base Address for that variable.

Similarly, a char variable takes just 1 box(1 byte), a float variable takes 4 boxes, a double variable takes 8 boxes and so on!


Operators working on pointers

Only two arithmetic operations, addition and subtraction, may be performed on a pointer. When you add 1 to a pointer, you are actually adding the size of whatever the pointer is pointing at. That is, each time a pointer is incremented by 1, it points to the memory location of the next element of its base type. To understand it more clearly, let us take the previous figure.

Now consider the following code :


int * iptr;         // create an int pointer

iptr = & a;       // make iptr point to a

iptr++;            // increment iptr

 

Let’s visualize and discuss every step:


Statement 1 :  It creates an integer pointer (i.e. a pointer pointing to an integer). As its not initialized, at present it is not pointing at anything. But being a variable, it does take space in the memory (8 bytes). Say it’s address is 100.

Look at the memory now.

 

Pointer Arithmetic
Pointer variable created at Base Address 100 
















Statement 2 : It stores the address of variable ‘a’ in the pointer iptr.

Pointer Arithmetic
Base address of variable stored in pointer
















Statement 3 : Now this is the important step. What happens when the pointer value is incremented? As one might think, the value stored in iptr is increased by one. So, it should point to the next memory block. But, after 117 the next three addresses (viz. 118, 119 & 120) are not free. They are integrated together to form the variable a. So, the pointer sees 121 as the next memory block. Being an int type pointer, iptr readily converts the box at 121 into an integer by including the next 3 boxes.

So, the value stored in iptr is 121 and the memory looks like the figure below:

 

Pointer Arithmetic
Memory after incrementation
















I hope the concepts are clear now. Also decrement operator works the similar way. Suppose an int pointer holds the address 100 then after decrementing once, it will point at 96 and not 99.


When a character pointer is incremented, its value increase by one or whatever the size of character on your machine; an integer pointer when incremented, its value increases by four (or whatever the size of integer on your machine); a float pointer when incremented, its value increases by four; and so on.

 

In pointer arithmetic, all pointers increase and decrease by the length of the data type they point to.

 

Not just 1 but other integers may also be added to or subtracted from pointers. When you give the following expression:

 

iptr += 2;

 

it will make iptr point to the 2nd element of iptr’s type (int here) beyond the one it is currently pointing to. That is, if iptr is currently pointing to memory location 1001, then iptr +2 will make it point to address 1009, which is the result of 1001 + size of (2 integers)=1001 + 8 = 1009.

 

Other arithmetic operations like multiplication and division are not permitted on pointers. Also, you may not add two pointers and only integers can be added or subtracted from pointers.

 

When we say that it is a char pointer or int pointer or float pointer, we mean to say that this pointer is holding address of a memory location that can hold char, or int or float value respectively. However, for char value, the memory location is 1 byte wide; for int it is 4 bytes; and 4 bytes for float. Whatever may be the pointer type, the pointer holds the address of the very first byte (known as the Base Address) of the memory location it points to.

 

Do relational operators act on pointers?

Yes, the relational operators == and != act on pointers. These operators can be used to check the pointer value against some other values, like checking if the pointer points to NULL or not.

See the following code for example :

 

#include<iostream>

using namespace std;

int main()

{

int * iptr = NULL;               // initializing iptr with NULL

if(iptr == NULL)                 // Using relational operator

cout << “It is NULL ”;        // Output statement

return 0;

}

 

Output:

 

It is NULL

 

Here another thing can be understood that dereferencing a NULL pointer causes error. This is due to the fact that a NULL pointer is not pointing at any valid memory address. So, dereferencing it doesn’t mean anything logical.

 

 

 

 

 

Do not stop here. Read all articles related to C++ here.

If you have any doubts, queries or suggestions for the website, please comment on this post.

If you want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will publish it on the website with your name.

Download the pdf form of this article here.

 

Related Topics :

Introduction to pointers

Static and Dynamic Allocation of memory

Declaration and Initialization of pointers

Dynamic Allocation Operators

Memory Leaks

Pointers & Constants


Post a Comment