Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Pointer Arithmetic

Pointer arithmetic in C allows you to perform mathematical operations on pointers, which is helpful for accessing elements in arrays and other contiguous memory blocks. The arithmetic operations you can use with pointers include incrementing (++), decrementing (--), addition (+), and subtraction (-). Understanding pointer arithmetic is essential for effective memory manipulation.

Basics of Pointer Arithmetic

  1. Incrementing/Decrementing a Pointer: Moving the pointer to the next or previous memory address based on the data type it points to.
  2. Adding/Subtracting an Integer: Moving the pointer by a specified number of elements.
  3. Difference Between Two Pointers: Useful for finding the number of elements between two pointers in an array.

Example of Pointer Arithmetic with an Array

In this example, we’ll demonstrate pointer arithmetic using an array of integers.

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr;  // Point to the first element of arr

    printf("Initial pointer points to: %d\n", *ptr);

    // Increment pointer
    ptr++;  // Move to the next element in the array
    printf("After incrementing, pointer points to: %d\n", *ptr);

    // Add 2 to pointer
    ptr += 2;  // Move 2 elements ahead
    printf("After adding 2, pointer points to: %d\n", *ptr);

    // Subtract 1 from pointer
    ptr -= 1;  // Move 1 element back
    printf("After subtracting 1, pointer points to: %d\n", *ptr);

    return 0;
}

Explanation

  1. Initialize Pointer: int *ptr = arr; assigns ptr to point to the first element of arr.
  2. Increment Pointer: ptr++ moves the pointer to the next integer in the array. If ptr was initially pointing to arr[0], it now points to arr[1].
  3. Adding an Integer: ptr += 2 advances the pointer by 2 elements. If it was at arr[1], it moves to arr[3].
  4. Subtracting an Integer: ptr -= 1 moves the pointer back by 1 element.

Output:

Initial pointer points to: 10
After incrementing, pointer points to: 20
After adding 2, pointer points to: 40
After subtracting 1, pointer points to: 30

How Pointer Arithmetic Works

When you perform arithmetic on a pointer, the compiler moves the pointer by the size of the data type it points to:

  • For example, if an int is 4 bytes, then ptr++ actually increases the memory address by 4 bytes, moving it to the next integer in the array.
  • Similarly, ptr + n moves the pointer by n * sizeof(data_type) bytes.

Example: Difference Between Pointers

The difference between two pointers can tell you how many elements separate them in an array:

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr1 = &arr[1];  // Point to arr[1]
    int *ptr2 = &arr[4];  // Point to arr[4]

    int diff = ptr2 - ptr1;  // Calculate the difference between pointers
    printf("Difference between ptr2 and ptr1: %d\n", diff);  // Output: 3

    return 0;
}

In this example, the difference between ptr2 and ptr1 is 3 because there are three elements (20, 30, and 40) between arr[1] and arr[4].

Important Notes on Pointer Arithmetic

  • Pointer arithmetic is only valid for pointers pointing to elements within the same array.
  • The pointer must not go out of the bounds of the allocated memory (doing so causes undefined behavior).
  • Pointer arithmetic works based on the size of the data type the pointer refers to (e.g., int, char, float, etc.).

Summary

Pointer arithmetic allows you to navigate through memory by:

  • Incrementing/Decrementing to move by one element at a time.
  • Adding/Subtracting integers to jump to specific elements.
  • Finding differences between pointers to determine the number of elements separating them in an array.

    Leave a Reply

    Your email address will not be published.

    Need Help?