C Programs Tutorials | IT Developer
IT Developer

C Programming - C Pointer Arithmetic



Share with a Friend

C Programming - C Pointer Arithmetic

C Pointer Arithmetic

Pointer arithmetic allows you to perform operations on pointers in C, such as incrementing, decrementing, and calculating the difference between pointers. Pointer arithmetic is primarily used to traverse arrays or perform operations on memory addresses directly.

Pointer Arithmetic Operations

  1. Incrementing a Pointer (++)
    • When you increment a pointer, it moves to the next memory location according to the size of the data type it is pointing to.

Example:

C

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

int *ptr = arr;

printf("First element: %d\n", *ptr);  // 10

ptr++;  // Move to the next element

printf("Second element: %d\n", *ptr);  // 20

  1. Decrementing a Pointer (--)
    • When you decrement a pointer, it moves to the previous memory location according to the size of the data type it is pointing to.

Example:

C

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

int *ptr = arr + 3;  // Point to the 4th element (40)

printf("Current element: %d\n", *ptr);  // 40

ptr--;  // Move to the previous element

printf("Previous element: %d\n", *ptr);  // 30

  1. Pointer Difference
    • You can subtract two pointers pointing to elements of the same array. The result is the number of elements between them.

Example:

C

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

int *ptr1 = &arr[1];  // Points to 20

int *ptr2 = &arr[4];  // Points to 50

printf("Difference: %ld\n", ptr2 - ptr1);  // 3 (since there are 3 elements between 20 and 50)

  1. Pointer Addition
    • You can add an integer to a pointer. This adds the integer times the size of the data type to the pointer.

Example:

C

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

int *ptr = arr;

ptr = ptr + 2;  // Move to the 3rd element (30)

printf("Third element: %d\n", *ptr);  // 30

Important Points About Pointer Arithmetic:

  1. Pointer Type Matters:
    • Pointer arithmetic depends on the size of the data type. When you increment or decrement a pointer, the pointer moves by the size of the data type it points to. For example, an int pointer will move by 4 bytes (on most systems), and a char pointer will move by 1 byte.

Example:

C

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

int *ptr = arr;

ptr++;  // Moves 4 bytes ahead (next integer)

printf("Next integer: %d\n", *ptr);  // 20

  1. Array and Pointer Relationship:
    • In C, the name of an array is essentially a pointer to its first element. Thus, pointers and arrays are closely related, and pointer arithmetic allows you to navigate through the array elements.

Example:

C

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

int *ptr = arr;

printf("First element: %d\n", *ptr);  // 10

ptr++;  // Move to the next element

printf("Second element: %d\n", *ptr);  // 20

  1. Pointers and Memory Allocation:
    • Pointer arithmetic is often used when working with dynamic memory allocation (e.g., with malloc or calloc). It allows for efficient memory manipulation.

Example of Pointer Arithmetic:

C

#include <stdio.h>

int main() {

    int arr[] = {1, 2, 3, 4, 5};

    int *ptr = arr;

    // Print elements using pointer arithmetic

    printf("Using pointer arithmetic:\n");

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

        printf("arr[%d] = %d\n", i, *(ptr + i));

    }

    // Pointer difference

    int *ptr1 = &arr[1];  // Points to 2

    int *ptr2 = &arr[4];  // Points to 5

    printf("\nDifference between ptr1 and ptr2: %ld\n", ptr2 - ptr1);  // 3 (3 elements between 2 and 5)

    return 0;

}

Output:

Using pointer arithmetic:

arr[0] = 1

arr[1] = 2

arr[2] = 3

arr[3] = 4

arr[4] = 5

Difference between ptr1 and ptr2: 3

Conclusion:

Pointer arithmetic is a useful feature in C that allows you to work directly with memory and manipulate data efficiently. It is most often used when dealing with arrays, dynamic memory, and linked lists. However, it requires careful handling since improper pointer arithmetic can lead to undefined behavior, such as accessing memory out of bounds or causing memory leaks.