Detailed Explanation of Pointer Arithmetic in C Language


Pointer arithmetic allows arithmetic operations on pointers in C, such as addition and subtraction. However, since pointers store addresses, performing arithmetic operations with integer types will result in a pointer. When subtracting one pointer from another, the result will be an integer value. In C, you can perform the following arithmetic operations on pointers:
  • Increment

  • Decrement

  • Addition

  • Subtraction

  • Comparison

Incrementing Pointer

If you increment a pointer by 1, it will start pointing to the next location. This is somewhat different from regular arithmetic operations because the pointer’s value will increase by the size of the data type it points to.

We can traverse an array by using the increment operation on the pointer in a loop, making the pointer point to each element of the array, performing some operations on it, and updating the pointer in the loop.

The rules for incrementing a pointer are as follows:

New Address = Current Address + i * Size of Data Type

Where i is the number of increments of the pointer.

32-bit System

For a 32-bit integer variable, the pointer will increase by 2 bytes.

👇Click to Receive👇
👉C Language Knowledge Resource Collection

64-bit System

For a 64-bit integer variable, the pointer will increase by 4 bytes.

Let’s look at an example of incrementing a pointer variable on a 64-bit system.

#include <stdio.h>
int main(){  int number = 50;  int *p; // Pointer to int  p = &number; // Store the address of number variable  printf("The address of p variable is %u \n", p);  p = p + 1;  printf("After increment: The address of p variable is %u \n", p); // In our example, p's address will increase by 4 bytes.  return 0;}

Output Result

The address of p variable is 3214864300 After increment: The address of p variable is 3214864304

Traversing Array with Pointer

#include <stdio.h>
void main () {  int arr[5] = {1, 2, 3, 4, 5};  int *p = arr;  int i;  printf("Printing array elements...\n");  for(i = 0; i < 5; i++) {    printf("%d ", *(p + i));  }}

Output Result

Printing array elements...1 2 3 4 5

Decrementing Pointer

Similar to incrementing, we can also perform decrement operations on pointers. If you decrement a pointer by 1, it will start pointing to the previous location. The formula for decrementing a pointer is as follows:

New Address = Current Address – i * Size of Data Type

32-bit System

For a 32-bit integer variable, the pointer will decrease by 2 bytes.

64-bit System

For a 64-bit integer variable, the pointer will decrease by 4 bytes.

Let’s look at an example of decrementing a pointer variable on a 64-bit operating system.

#include <stdio.h>
void main() {  int number = 50;  int *p; // Pointer to int  p = &number; // Store the address of number variable  printf("The address of p variable is %u \n", p);  p = p - 1;  printf("After decrement: The address of p variable is %u \n", p); // p now points to the previous location.}

Output Result

The address of p variable is 3214864300 After decrement: The address of p variable is 3214864296

Pointer Addition in C

We can add a value to a pointer variable. The formula for pointer addition is as follows:

  1. New Address = Current Address + (Number * Size of Data Type)

32-bit System

For a 32-bit integer variable, it will increase by 2 * Number.

64-bit System

For a 64-bit integer variable, it will increase by 4 * Number.

Let’s look at an example of adding a value to a pointer variable on a 64-bit architecture.

#include <stdio.h>
int main(){  int number = 50;  int *p; // Pointer to int  p = &number; // Store the address of number variable  printf("The address of p variable is %u \n", p);  p = p + 3; // Add 3 to the pointer variable  printf("After adding 3: The address of p variable is %u \n", p);  return 0;}

Output Result

The address of p variable is 3214864300 After adding 3: The address of p variable is 3214864312

As you can see, p’s address is 3214864300. However, after adding 3 to the p variable, it becomes 3214864312, which is an increase of 12 bytes (4 * 3). Since we are using a 64-bit architecture, it increased by 12 bytes. However, if we were using a 32-bit architecture, it would only increase to 6, i.e., 2 * 3 = 6. Because in a 32-bit operating system, integer values occupy 2 bytes of memory.

Pointer Subtraction in C

Similar to pointer addition, we can subtract a value from a pointer variable. Subtracting any number from a pointer will yield an address. The formula for pointer subtraction is as follows:

New Address = Current Address – (Number * Size of Data Type)

32-bit System

For a 32-bit integer variable, it will decrease by 2 * Number.

64-bit System

For a 64-bit integer variable, it will decrease by 4 * Number.

Let’s look at an example of subtracting a value from a pointer variable on a 64-bit architecture.

#include <stdio.h>
int main(){    int number = 50;    int *p; // Pointer to int    p = &number; // Store the address of number variable    printf("The address of p variable is %u \n", p);    p = p - 3; // Subtract 3 from the pointer variable    printf("After subtracting 3: The address of p variable is %u \n", p);    return 0;}

Output Result

The address of p variable is 3214864300 After subtracting 3: The address of p variable is 3214864288

You can see that after subtracting 3 from the pointer variable, it is 12 smaller than the previous address value (4 * 3).

However, we can also subtract one address (pointer) from another address (pointer). This will yield a number rather than a simple arithmetic operation, but it follows the following rule.

If two pointers have the same type,

Address2 – Address1 = (Difference of the two addresses) / Size of the data type pointed to by the pointer

Consider the following example to subtract one pointer from another pointer.

#include <stdio.h>
void main () {    int i = 100;    int *p = &i;    int *temp;    temp = p;    p = p + 3;    printf("Pointer subtraction: %d - %d = %d", p, temp, p-temp);}

Output Result

Pointer subtraction: 1030585080 - 1030585068 = 3

Illegal Pointer Operations

There are some operations that cannot be performed on pointers. Since pointers store addresses, we must ignore operations that may lead to illegal addresses, such as addition and multiplication. Here is a list of such operations.

  • Address + Address = Illegal

  • Address * Address = Illegal

  • Address % Address = Illegal

  • Address / Address = Illegal

  • Address & Address = Illegal

  • Address ^ Address = Illegal

  • Address | Address = Illegal

  • ~Address = Illegal

Pointer to Function in C

As we discussed in previous chapters, pointers can point to functions in C. However, the declaration of the pointer variable must match the function. Consider the following example to make a pointer point to a function.

#include <stdio.h>
int addition();int main() {    int result;    int (*ptr)();    ptr = &addition;    result = (*ptr)();    printf("Sum is %d", result);}int addition() {    int a, b;    printf("Enter two numbers?");    scanf("%d %d", &a, &b);    return a + b;}

Output Result

Enter two numbers?3 5Sum is 8

In the above example, we declared a function named addition. Then, we declared a pointer variable ptr that points to a function of the same type as addition. We used &addition to point ptr to the address of the addition function. Then, we called the addition function by adding * before ptr and stored its result in the result variable. Finally, we printed the result using printf.

Detailed Explanation of Pointer Arithmetic in C LanguageProgrammer Technical Exchange GroupDetailed Explanation of Pointer Arithmetic in C Language

Scan the code to join the group, remember to note: city, nickname, and technical direction.



Leave a Comment