Pointer Arithmetic in C Language

Pointer-Arithmetic-in-C-programming-Language

Introduction to Pointer Arithmetic in C:

We have looked at the Introductions to pointers in our previous article, In today’s article we will look at pointer arithmetic in c programming language. and what arithmetic operations we can perform on pointers with example programs.

Pointer Arithmetic in C Programming:

As pointers store the memory address of another variable. So We can’t perform all arithmetic operations on the pointer similar to the other normal variables.

The valid operations on pointers are

  • Incrementing the Pointer variable
  • Decrementing the pointer variable
  • Adding an Integer to Pointer
  • Subtracting an Integer from Pointers
  • Subtraction of Pointer variable from another pointer variable
  • Finally, We can also perform a Comparison of two pointer variables. ( Like == , !=, <, >, ≤ , and >= )

We can’t perform the below operations on the pointers. Invalid Operations on Pointer variables are

  • Addition of Two Pointers
  • Multiplication of Two Pointers
  • Division of Two Pointers

Let’s look at the above pointer arithmetic in C language in detail.

Incrementing Pointer in C Language:

Let’s say we have a pointer variable called intPtr, which is an integer pointer and it is pointing to the memory address of the num integer variable.

Let’s say the memory address of the num is equal to 0x1000 also the size of the integer variable is 4 Bytes.

In General, The increment operation increases the value of the variable by one. Similarly, As the pointer variable contains the memory address, incrementing it increases the memory address value by N bytes. Where N is the size of the pointer variable data type.

In the above case, Incrementing the intPtrcause the pointer variable to point to the memory address 0x1004. This is because the pointer datatype is of integer type, and the size of an integer is 4 bytes.

📢Incrementing the integer pointer increases the pointer value by 4 Bytes

Before the increment operation pointer value is
0x1000

intPtr++;

After the increment operation pointer value is 0x1004

Let’s look at another example to understand the pointer increment operation. Incrementing the character pointer variable.

Here we have created a character ch and character pointer chPtr and the chPtr is holding the address of the ch variable.

Let’s say the address of the ch variable is 0x9990, Then chPtr is currently holding the 0x9990.

If we increment the chPtrpointer variable, Then the chPtrpointer value will be raised by the number of bytes equal to the character size. As the character size is 1 Byte, So the pointer value will be equal to 0x9991 after the increment operation.

📢Incrementing the character pointer increases the pointer value by 1 Byte

Before the increment operation pointer value is 0x9990

chPtr++;

After the increment operation pointer value is 0x9991

📢The increment operation on the pointer variable increases the pointer value by the number of bytes of pointers datatype.

Program to understand the Increment pointer in c:

The following program demonstrates the behavior of pointer increment operation in C language.

Program Output:

Compile and Run the program.

program-to-increment-pointer-in-c-language

Pointer Increment Program Explanation:

In the above program, We have created an integer variable num and integer pointer intPtr. We also created the character variable ch and character pointer chPtr.

  • The integer pointer intPtr is holding the address of num
    • The initial value of the intPtr is 0x7fff143c9e04
    • Then we performed the increment operation – intPtr++;
    • The value of the integer pointer is increased by 4 bytes and The intPtr value after the increment operation is 0x7fff143c9e08. If you observe the memory addresses the value is increased by 4 Bytes ( which is size of the integer in my present machine)
  • Similarly, The character pointer chPtr is storing the address of ch.
    • Initial value of the chPtris 0x7fff143c9e03
    • Then we incremented the chPtr by 1 – chPtr++;
    • The resultant value of the chPtr is 0x7fff143c9e04, As the character size is equal to 1 Byte, The character pointer value is increased by 1 Byte due to the increment operation

Decrementing a pointer variable in C – Pointer arithmetic in c:

The decrementing the pointer variable will decrease the value of the pointer by N Bytes ( where N is the size of the pointer datatype)

This is very similar to the above pointer increment operation. Let’s look at an example.

Let’s say the value of the pricePtris equal to 0x4450, which means the address of the price variable is 0x4450

Decrement the pricePtr pointer variable.

By decrementing the pointer variable pricePtr, The value of the pointer will be reduced by the 4 Bytes,( As the size of the int is 4 Bytes).

So the pricePtr will contain the value 0x4446 after the decrement operation.

Example to demonstrate the pointer decrement operation:

Program Output:

Let’s compile and run the above program.

program-to-decrement-pointer-in-c-language

As we can see from the above output, The pointer decrement operation decreased the integer pointer( pricePtr) value by 4 Bytes ( 0x7ffd08d7c7c4 to 0x7ffd08d7c7c0)

Similarly, Decrementing the character pointer reduced the character pointer( cPtr) value by 1 Byte ( 0x7ffd08d7c7c3 to 0x7ffd08d7c7c2)

📢We use the pointer increment and decrement operators heavily to traverse the Arrays and strings in C.

Adding an integer to a pointer in c language:

We can add an integer or number to a pointer variable. Incrementing the pointer variable once increased the pointer value by N bytes ( where N is the size of the pointer datatype)

So if we add an integer or number M to the pointer, It will increase the pointer value by M * N Bytes. ( where N is the size of the pointer datatype).

Let’s look at an example.

In the above example, We created an integer variable age and a pointer variable agePtr. The agePtr is storing the address of the age variable. Then we added an integer 2 to the agePtr pointer variable.

Let’s say the address of the age integer variable is 0x5000 , So the agePtr is holding the 0x5000 as the value.

If we add the integer 2 to the agePtr, It will increase the agePtr value by 8 Bytes. ( 2 * Size of Integer which is 2 * 4 )

Similarly, If we add 5 to agePtr, It will increase the agePtr value by 20 Bytes.

Adding an Integer to Pointer in C Program:

Let’s look at a program to understand how Adding an Integer to a pointer works in C programming language.

Save the above program as pointer-addition.c

Program Output:

Compile and Run the above pointer-addition.c program using GCC compiler (Any compiler)

Adding-integer-to-pointer-in-c-program-output

If you notice, The agePtr value is increased by the 8 Bytes. As the above addresses are in hexadecimal values, We need to convert them to decimal values to see the difference.

0C is equal to 12.

14 is equal to 20.

As we can see, The agePtr value is increased by 8 Bytes.

Subtracting an Integer from a Pointer variable in C:

Similar to the above integer and pointer addition operation, Subtracting an integer M from the pointer will reduce the pointer value by M * N Bytes. ( where N is the size of the pointer datatype).

So if you remove 3 from the integer pointer, It will reduce the pointer value by 12 Bytes

📢Adding or subtracting an integer from a pointer results in a pointer variable.

Here is the program to understand the pointer and the integer subtraction operation

Program to demonstrate Pointer and Integer Subtraction Operation in C:

Program Output:

Compile the program

$ gcc pointer-subtraction.c

Run the program.

subtracting-integer-to-pointer-in-c-program-output

In the above program, We subtracted the number 3 from the integer pointer subPtr. So the value of the integer pointer is reduced by 12 Bytes. ( 3 * 4 = 12 ) .

Subtract Two Pointers in C language:

C Programming language allows us to subtract two pointers of the same data type. The subtraction of pointers will give us how far two pointers are located in the memory. Often pointer subtraction is used to calculate the number of elements(of the same type) between the two memory addresses.

For example, If an integer pointer( ptr1) is pointing to the memory address 0x1008 and another integer pointer( ptr2) is pointing to 0x1000, Then the subtraction of pointers ptr1 - ptr2 will return value 2. which is equal to the 2 integer places ( assuming integer size as 4 Bytes).

📢Subtraction of two pointers results in an integer value ( long int)

Program to Subtract Two Pointers in C Programming:

Let’s look at the example program to subtract two pointers in c programming language.

Program Output:

Let’s compile and Run the program and observe the output.

subtract-two-pointers-in-c-language

As we can see from the above output, The ptr2 value is 0x7fff1821f3c4 and ptr1 value is 0x7fff1821f3c0. So the subtraction of two pointers ptr2-ptr1 is 1. ( As the ptr2 and ptr1 are integer pointers)

Comparison of Two Pointers in C language:

As we specified earlier, The c programming language supports the comparison of two pointer variables.

The allowed comparison operations are

  • Equality Operator==
  • Not Equal(Inequality) Operator !=
  • Less than Operator <
  • Greater than Operator >
  • Less than and Equal operator <=
  • Greater than an equal operator >=

Let’s look at a program to check if two pointers are equal.

C Program to Check Two Pointers are Equal:

The following program compares pointer variables and displays the results.

We created three-pointers in the above program and stored the addresses of the two numbers. The ptr1 and ptr3 storing the address of the n1 variable and the ptr2 is storing the address of the n2 variable. Finally, We compared the pointer variables i.e Compared the pointers ptr2 and ptr3 with the pointer ptr1.

Program Output:

Compile and Run the above C Program.

compare-two-pointers-in-c-program-output

As we can see from the above output, The pointers ptr1 and ptr2 are pointing to different variables so their memory address is different. So the ptr1 and ptr2 are not equal.

Similarly, The ptr1 and ptr2 are pointing to the same variable n1, So they are equal.

Pointer Arithmetic in C – Conclusion:

In this article, We have looked at the pointer arithmetic in detail. We looked at the incrementing and decrementing pointers in C, We then discussed how to compare pointers, as well as how to add and subtract integer values from pointers.

Related C Tutorials:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] have looked at the introduction to pointers, and pointer arithmetics in earlier articles. In today’s article, We will look at the pointer to pointer in c language or […]

  2. […] Pointer Arithmetic in C Language […]

Leave a Reply