Pointer Arithmetics in C with Examples

Pointer Arithmetics in C

Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn’t store any value. 

Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer arithmetic operations are slightly different from the ones that we generally use for mathematical calculations. These operations are:


1. Increment

2. Decrement

3. Addition

4. Subtraction

5. Comparison

1. Incrementing Pointer in C

Increment a pointer to move it to the next memory location. This is often used when working with arrays, as it allows you to access the next element in the array. Here's how you can increment a pointer in C:
c
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Initialize the pointer to the first element of the array
// Access and print the elements using pointer increment
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *ptr);
ptr++; // Increment the pointer to point to the next element
}
return 0;
}

In this example, we start with a pointer ptr pointing to the first element of the integer array arr. We use the ptr++ operation inside the loop to increment the pointer, which effectively moves it to the next element in the array. As a result, we can access and print each element of the array using the incremented pointer.

Output:
mathematica
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50


The ++ operator increments the pointer by the size of the data type it points to. In this case, it's an int, so each increment moves the pointer by sizeof(int) bytes.

2. Decrementing Pointer in C


Decrement a pointer to move it to the previous memory location. This is often used when working with arrays, allowing you to access the previous element in the array. Here's how you can decrement a pointer in C:
c
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = &arr[4]; // Initialize the pointer to the last element of the array
// Access and print the elements using pointer decrement
for (int i = 4; i >= 0; i--) {
printf("Element %d: %d\n", i, *ptr);
ptr--; // Decrement the pointer to point to the previous element
}
return 0;
}


In this example, we start with a pointer ptr pointing to the last element of the integer array arr. We use the ptr-- operation inside the loop to decrement the pointer, which effectively moves it to the previous element in the array. As a result, we can access and print each element of the array using the decremented pointer.

Output:
mathematica
Element 4: 50
Element 3: 40
Element 2: 30
Element 1: 20
Element 0: 10


The -- operator decrements the pointer by the size of the data type it points to. In this case, it's an int, so each decrement moves the pointer by sizeof(int) bytes in the reverse direction.

3. Addition Pointer in C:


When a pointer is added with an integer value, the value is first multiplied by the size of the data type and then added to the pointer

c
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("Initial pointer value: %p\n", ptr);
// Move the pointer to the next element in the array
ptr += 2;
printf("After adding 2: %p, value: %d\n", ptr, *ptr);
return 0;
}


Output:

yaml
Initial pointer value: 0x7fff5dbb8010 
 adding 2: 0x7fff5dbb8018, value: 40


4. Subtraction Pointer in C:

You can find the difference between two pointers to determine the number of elements between them.
c
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr1 = &arr[1];
int *ptr2 = &arr[4];
int diff = ptr2 - ptr1;
printf("Difference between ptr2 and ptr1: %d\n", diff);
return 0;
}


Output:

sql
Difference between ptr2 and ptr1: 3


5. Comparison Pointer in C:

We can compare the two pointers by using the comparison operators in C. We can implement this by using all operators in C >, >=, <, <=, ==, !=. It returns true for the valid condition and returns false for the unsatisfied condition.

c
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr1 = &arr[2];
int *ptr2 = &arr[4];
int *ptr3 = &arr[0];
// Equality comparison
if (ptr1 == ptr2) {
printf("ptr1 and ptr2 are equal\n");
} else {
printf("ptr1 and ptr2 are not equal\n");
}
if (ptr1 == ptr3) {
printf("ptr1 and ptr3 are equal\n");
} else {
printf("ptr1 and ptr3 are not equal\n");
}
// Relational comparisons
if (ptr1 < ptr2) {
printf("ptr1 comes before ptr2\n");
} else {
printf("ptr1 does not come before ptr2\n");
}
if (ptr2 > ptr3) {
printf("ptr2 comes after ptr3\n");
} else {
printf("ptr2 does not come after ptr3\n");
}
return 0;
}

Output:

sql
ptr1 and ptr2 are not equal
ptr1 and ptr3 are not equal
ptr1 comes before ptr2
ptr2 comes after ptr3

Pointer Arithmetic with Arrays:


    Pointers can be used to traverse arrays and access elements.

    c
    #include <stdio.h>
    int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr;
    for (int i = 0; i < 5; i++) {
    printf("Element %d: %d\n", i, *ptr);
    ptr++;
    }
    return 0;
    }


    Output:

    mathematica
    Element 0: 10
    Element 1: 20
    Element 2: 30
    Element 3: 40
    Element 4: 50

    Post a Comment

    0 Comments