Programmer Coding

Pointer Arithmetic in C Language

Pointer Arithmetic?

We can perform arithmetic operations such as addition and subtraction of indicators. However, we know that the pointer has an address and if the other operand is an integer type, the result of the arithmetic operation to be performed on the pointer will also be a pointer. Subtracting meters from meters will result in a number. Pointers in C can perform the following arithmetic operations:

  • Increment
  • Decrement
  • Addition
  • Subtraction
  • Comparison

Incrementing Pointer in C

If we add 1 to the index the pointer will start pointing to the next immediate location. This differs slightly from ordinary arithmetic in that the value of the parameter increases with the size of the data type the parameter contains.

We can iterate an array by incrementing the pointer, which will point to each element of the array, perform some operations on it, and update itself in a loop.

The Rule to increment the pointer is given below:

  1. new_address= current_address + i * size_of(data type)

Where i is the number by which the pointer get increased.

32-bit

For 32-bit int variable, it will be incremented by 2 bytes.

64-bit

For 64-bit int variable, it will be incremented by 4 bytes.

Let’s see the example of incrementing pointer variable on 64-bit architecture.

  1. #include<stdio.h>

    int main() {
    int number = 50;
    int *p; // pointer to int

    p = &number; // stores the address of number variable

    printf(“Address of p variable is %p\n”, p);

    p = p + 1; // incrementing the pointer by 1

    printf(“After increment: Address of p variable is %p\n”, p);

    return 0;
    }

Output

 

Address of p variable is 3214864300

After increment: Address of p variable is 3214864304

#include <stdio.h>

int main() {

int arr[] = {10, 20, 30, 40, 50};

int *ptr = arr; // Initialize pointer to the start of the array

printf(“Array elements: “);    // Traverse the array using pointer

for (int i = 0; i < 5; i++) {

printf(“%d “, *ptr); // Print the value pointed to by ptr

ptr++; // Move pointer to the next element

}

return 0;

}

Output

Array elements: 10 20 30 40 50

C Pointer Addition

Pointer insertion in C is the process of incrementing or decrementing a pointer to navigate through an array or block of memory. When you add an integer value to the index, it adjusts the memory address of the index according to the size of the data type it points to.

Syntax

new_address = current_address + (number * size_of(data_type))

32-bit

For 32-bit int variable, it will add 2 * number.

64-bit

For 64-bit int variable, it will add 4 * number.

Let’s see the example of adding value to pointer variable on 64-bit architecture.

Example

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Initialize pointer to the start of the array

printf(“Original address: %p\n”, ptr); // Print the original address

// Increment pointer by 1 (sizeof(int)) to move to the next integer element
ptr++;

printf(“New address after increment: %p\n”, ptr); // Print the new address
return 0;
}

Original address: 0x7ffee5b2dc90
New address after increment: 0x7ffee5b2dc94

C Pointer Subtraction

As an additional indicator, we can subtract the value from the index variable. Subtracting a digit from the pointer gives the address. The formula for subtracting the value from the measured variable is as follows:

  1. new_address= current_address – (number * size_of(data type))

32-bit

For 32-bit int variable, it will subtract 2 * number.

64-bit

For 64-bit int variable, it will subtract 4 * number.

Let’s see the example of subtracting value from the pointer variable on 64-bit architecture.

#include<stdio.h>

intmain(){

intnumber=50;

int*p;//pointer to int

p=&number;//stores the address of number variable

printf(“Address of p variable is %u \n”,p);

p=p-3; //subtracting 3 from pointer variable

printf(“After subtracting 3: Address of p variable is %u \n”,p);

return0;

}

Output

Address of p variable is 3214864300

After subtracting 3: Address of p variable is 3214864288

Illegal arithmetic with pointers

There are various operations which can not be performed on pointers. Since, pointer stores address hence we must ignore the operations which may lead to an illegal address, for example, addition, and multiplication. A list of such operations is given below.

  • 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

Pointers to functions in C as we discussed in the previous section. However, the declaration of the pointer variable must be the same as the function. To create a benchmark for this activity, consider the following example.

Example

#include <stdio.h>

// Function prototype

int addition();

int main() {

int result;

int (*ptr)(); // Declare a function pointe

// Assign the address of the addition function to the function pointer

ptr = &addition;

// Call the function through the function pointer and store the result

result = (*ptr)();

// Print the result

printf(“The sum is %d\n”, result);

return 0;

}

// Definition of the addition function

int addition() {

int a, b;

// Input two numbers

printf(“Enter two numbers: “);

scanf(“%d %d”, &a, &b);

// Return the sum of the two numbers

return a + b;

}

Output

Enter two numbers: 10 20

The sum is 30

Pointer to Array of functions in C

To understand the concept of array operations we need to understand array operations. Basically a function array is an array containing the addresses of functions. In other words, a pointer to an array of functions is a pointer to an array containing function elements. Consider the following example.

#include <stdio.h>

// Function prototypes

int show();

int showadd(int);

// Function pointer array and pointer to array

int (*arr[3])();

int (*(*ptr)[3])();

int main() {

int result1;

// Assign functions to array elements

arr[0] = show;

arr[1] = showadd;

// Assign address of array to pointer to array

ptr = &arr;

// Call show function through pointer to array and store the result

result1 = (**ptr)();

// Print the value returned by show

printf(“Printing the value returned by show: %d\n”, result1);

// Call showadd function through pointer to array and pass result1 as argument

(*(*ptr + 1))(result1);

return 0;

}

// Definition of show function

int show() {

int a = 65;

return a++;

}

// Definition of showadd function

int showadd(int b) {

printf(“Adding 90 to the value returned by show: %d\n”, b + 90);

}

Output

Printing the value returned by show: 65

Adding 90 to the value returned by show: 155

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top