# Pointer Arithmetic, Pointer Size, and Pointer Type in C and C++

In this tutorial, we will learn about pointer arithmetic operations such as addition, subtraction. In order to understand pointer arithmetic, first, we must have a clear understanding of pointer size and how pointer type affects the operation of pointer arithmetic operation.

If you are looking for a In depth getting started guide on Pointers, check on this link:

Introduction to Pointers

## Pointer Size

The size of pointer is constant for all data types such as character, integer and even structures. The size of pointer depends on the machine we are using. For example, in a 32-bit machine, the pointer size is 4 bytes and in 64-bit machine, the pointer size is 8 bytes. It also depends on the operating system and a compiler, if you compile your code on a operating system that supports 32-bit, pointer size will be 4 bytes i.e. 32/8 = 4 byes. Similarly, for 64-bit operating system, the pointer size will be 8 bytes.

But the most important thing is that the size of the pointer does not make any difference in pointer arithmetic. Because a pointer only holds the base or starting address of a variable.

For example, a Ptr is a pointer variable that points to an integer x. Because integer type variable takes 4 bytes of memory space in a typical machine and each memory location can save only one byte. Hence, integer variable x store in four consecutive memory locations and the pointer variable Ptr will only hold the address of the first byte that is the starting address of x as shown in the figure below:

According to above figure, the value of integer variable x is 15 which is equivalent to 0000 0000 0000 0000 0000 0000 0000 1111 in binary. If we consider a little-endian storage scheme, that means least significant bits store at lowest address location and most significant bits store at highest address location. Therefore, the binary value 0000 1111 stores at location 200 and rest of the values are binary zero. The pointer variable Ptr only hold the address of first byte that is 200.

## Role of Pointer Type

Only the type of pointer matters. Because the type of pointer determines the number of memory locations, it should forward or backward it points to in case of pointer increment and decrement.  The type of pointer also important because we use pointer to access or modify data from these addresses. Moreover, different data types have different sizes and type of pointer helps to dereference value from memory according to data type.

For example, if we print the value of variable x with *Ptr using print statement like this:

``printf(“%d”, *Ptr);``

Program sees that Ptr is a pointer to an integer that means machine needs to copy four bytes from memory starting from location 200. Hence, pointer type helps machine to define the number memory locations from which data must be copied.

## Pointer Arithmetic Basics

When we add or subtract any number from a pointer, it is not a simple arithmetic addition or subtraction. The amount by which pointer incremented or decremented depends on the type of pointer that is the type of data type to which it points to.

For instance, if it is pointer to a character and we increment pointer by one, it will be increment by one. If it is a pointer to an integer, it will be incremented by 4 and so on for other data types. This tables shows the effect of pointer increment operation on different types of pointers:

According to above table, when we increment a pointer by a specific number, the amount to be added is equal to the product of number and size of the pointer data type.

### Pointer Addition Basic Example in C / C++

To understand this concept let us take an example code. In this example, we declare an integer variable with A and initialized it with value 1025. After that, we declare a character variable and assign it a starting address of integer variable A.

``````#include<stdio.h>
#include<stdlib.h>
void main(void) {
int A = 1025;
char * Ptr = &A;
printf("Byte 1 address = %d , Byte 1 value = %d\n", Ptr, *Ptr);
Ptr = Ptr + 1;
printf("Byte 2 address = %d , Byte 2 value = %d\n", Ptr, *Ptr);
Ptr = Ptr + 1;
printf("Byte 3 address = %d , Byte 3 value = %d\n", Ptr, *Ptr);
Ptr = Ptr + 1;
printf("Byte 4 address = %d , Byte 4 value = %d\n", Ptr, *Ptr);

}``````

After that, we use print statements to print the value and address of each byte of integer variable A. Because Ptr is a pointer to a character type. Hence, for each increment to ptr, the value which will be added to Ptr is one. Therefore, the Pointer variable will point to the next byte of integer variable A on each increment.

#### How code Works?

First, we assign the address of integer variable A to the Pointer variable Ptr. Now, Ptr will point to address 200. Hence, when the first print statement executes, it will display address 200 and value 1.

``````int A = 1025;
char * Ptr = &A;
printf("Byte 1 address = %d , Byte 1 value = %d\n", Ptr, *Ptr);``````

These lines increment the pointer by one and again print value and address. Because the pointer Ptr is of character type. Hence, it will increment by one and point to the next byte of integer variable A.

``````Ptr = Ptr + 1;
printf("Byte 2 address = %d , Byte 2 value = %d\n", Ptr, *Ptr);``````

Similarly, Ptr will increment by one each increment operation and print statement display crossponding value and addresses.

On last print statement:

#### Code Output

Now let’s see the code output. After the compilation of the above code, you will get this output. As you can see the difference of address between each byte is 1. Moreover, the byte1 value is 1, and the byte 2 value is 4.

``````Byte 1 address = 11532740 , Byte 1 value = 1
Byte 2 address = 11532741 , Byte 2 value = 4
Byte 3 address = 11532742 , Byte 3 value = 0
Byte 4 address = 11532743 , Byte 4 value = 0``````

## Pointer Arithmetic Operations

Pointer support four arithmetic operations and each operation has many applications. For example, we can use Pointer subtraction operation to find a lenght of an array. Following four arithemtic operation can be performed on pointers:

• Adding a number to the pointer
• Subtracting an integer from a pointer
• Subtracting two pointers from each other
• Comparing pointers

To understand these pointer arithmetic operations. Let us take this example code. In this example, we initialize an array of 5 integers. Two integer type pointers also declared. one integer type pointer points (ptr1) to the first element of the array and second one (ptr2) points to the last element of the array. Array stores in memory in four contiguous location and each integer takes bytes of memory space. Therefore, the difference of address between each element of array is 4 bytes as shown in figure below:

### Pointer Increment Operation

In this example code, we display each element of integer array using pointer increment operation. Pointer ptr1 is a pointer integer that is first element to array. Hence, on each pointer increment 4 will be added to address and it will point to the next element of the array.

``````#include<stdio.h>
#include<stdlib.h>

void main(void) {
int x = { 10, 15, 20, 25, 30 };
int* ptr1 = &x;
int* ptr2 = &x;
for (int i = 0; i <= 4; i++) {
printf("address of x[%d]  = %d , x[%d] value = %d\n", i, ptr1, i, *ptr1);
ptr1++; // pointer increment by 1
}
}``````

The output of above shows that after each increment operation, ptr1 points to next array location. Also note that the difference of address between two contigous array elements is 4 bytes which proves that in case of integer pointer, increment of one, adds 4 to the address.

``````address of x  = 15726508 , x value = 10
address of x  = 15726512 , x value = 15
address of x  = 15726516 , x value = 20
address of x  = 15726520 , x value = 25
address of x  = 15726524 , x value = 30``````

### Pointer Decrement Operation

In this example code, we print pointer values using decrement operation. ptr2 points to last element of array. Hence, on each decrement, pointer moves to the previous element of array. In other words, decrement operation, subtracts four from the current address to which pointer points.

``````#include<stdio.h>
#include<stdlib.h>

void main(void) {
int x = { 10, 15, 20, 25, 30 };
int* ptr1 = &x;
int* ptr2 = &x;
for (int i = 4; i>=0 ; i--) {
printf("address of x[%d]  = %d , x[%d] value = %d\n", i, ptr2, i, *ptr2);
ptr2--;
}
}``````

As expected, first it prints the last element of the array x and its address. After each decrement operation, the number four is subtracted for the current address, and the pointer points to one position back element.

``````address of x  = 9698728 , x value = 30
address of x  = 9698724 , x value = 25
address of x  = 9698720 , x value = 20
address of x  = 9698716 , x value = 15
address of x  = 9698712 , x value = 10``````

### Subtraction Operation of two Pointers

If we subtract two pointers, it will just give output in the form of a number. For example in this figure, the address of pointer ptr2 is 220 and ptr1 is 204.

If we subtract these two pointers, we will get output 16. That means the difference of bytes between two pointers is 16 bytes. But, both pointers point to integers, hence, the compiler will divide this by 4 and we will get digit 4 as an output. Here, the 4 tells us the number of array elements between x and x.

``i = ptr2 - ptr1 = 220 - 204 = 16 / 4 = 4``
``````#include<stdio.h>
#include<stdlib.h>

void main(void) {
int x = { 10, 15, 20, 25, 30 };
int* ptr1 = &x;
int* ptr2 = &x;
printf("difference between ptr1 and ptr2 = %d", ptr2 - ptr1);
}
``````

Code output:

``difference between ptr1 and ptr2 = 4``

Note: We can use pointer subtraction to find the length of an array.

### Pointers Comparison

As we saw in last sections, pointers support limited arithmetic operations such as decrement, increment, addition and subtraction by a constant number and subtracting two pointers from each other. Moreover, pointers also support comparison operation such as less than or greater than operation. Although, comparing two pointers are not as useful as other operation that we perform on pointers. But we can use comparison feature of pointers find the relative ordering of an array elements.

When we use greater than or less than symbols to compare pointers, it gives output either ‘0’ or ‘1’. It gives output ‘1’, if condition is true and otherwise ‘0’.

This code compares different addresses of different elements of array and gives output in 0 or 1.

``````#include<stdio.h>
#include<stdlib.h>
void main(void) {
int x[] = { 10, 15, 20};
int* ptr1 = &x;
int* ptr2 = &x;
int* ptr3 = &x;
printf("%d\n", ptr1 > ptr2); // 0 output
printf("%d\n", ptr2 > ptr3); // 0 output
printf("%d\n", ptr2 > ptr1); // 1 output
}
``````

Similarly, we can also compare values of array elements using pointer deference operators.

``````#include<stdio.h>
#include<stdlib.h>
void main(void) {
int x[] = { 15, 10, 20};
int* ptr1 = &x;
int* ptr2 = &x;
int* ptr3 = &x;
printf("%d\n", *ptr1 > *ptr2); // 1 output
printf("%d\n", *ptr2 > *ptr3); // 0 output
printf("%d\n", *ptr2 > *ptr1); // 0 output
}
``````