Pointer Arithmetic in C

Hello there! Welcome to this tutorial on “Pointer Arithmetic in C”. If you’re here, you’re probably curious about pointers, those mysterious entities in C that hold addresses rather than values. Well, you’re in the right place! Let’s dive in, shall we?

Understanding Pointers

First things first, what’s a pointer? In C, a pointer is a variable that stores the memory address of another variable. This means that instead of holding a specific value, like an integer or a character, a pointer points to the location of a value in memory.

The Magic of Pointer Arithmetic

Now, here’s where things get interesting. In C, you can perform arithmetic operations on pointers, just like you can with regular numbers. This is known as pointer arithmetic. But wait, how can you perform arithmetic on addresses? Good question! Let’s find out.

Increment and Decrement of a Pointer

When you increment a pointer, you’re essentially moving the pointer to the next memory location. For instance, if you have a pointer ptr pointing to an integer, doing ptr++ will move the pointer to the next integer in memory. Similarly, ptr-- will move the pointer to the previous integer.

Here’s a code example to illustrate this:

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *ptr = arr; // ptr points to the first element of arr

    printf("Before increment: %d\n", *ptr);
    ptr++; // Now, ptr points to the second element of arr
    printf("After increment: %d\n", *ptr);

    return 0;
}
C

Addition and Subtraction of an Integer to a Pointer

You can also add or subtract an integer to a pointer. When you do this, the pointer moves forward or backward by a number of memory locations equal to the integer. For instance, ptr + 2 will move the pointer two memory locations forward.

Here’s a code example to illustrate this:

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *ptr = arr; // ptr points to the first element of arr

    printf("Before addition: %d\n", *ptr);
    ptr = ptr + 2; // Now, ptr points to the third element of arr
    printf("After addition: %d\n", *ptr);

    return 0;
}
C

Subtracting Two Pointers

Subtracting two pointers gives you the number of elements between them. For instance, if ptr1 and ptr2 are pointers to elements in the same array, ptr2 - ptr1 will give you the number of elements between the elements pointed to by ptr1 and ptr2.

Here’s a code example to illustrate this:

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *ptr1 = &arr[0]; // ptr1 points to the first element of arr
    int *ptr2 = &arr[3]; // ptr2 points to the fourth element of arr

    int diff = ptr2 - ptr1;
    printf("Difference between pointers: %d\n", diff);

    return 0;
}
C

Code Examples

Let’s look at a couple of complete C codes to understand pointer arithmetic better.

Code Example 1: Traversing an Array Using Pointer Arithmetic

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *ptr = arr; // ptr points to the first element of arr

    // Print all elements of arr using pointer arithmetic
    for(int i = 0; i < 5; i++) {
        printf("%d ", *(ptr + i));
    }

    return 0;
}
C

In this code, we’re using pointer arithmetic to traverse an array. The pointer ptr points to the first element of the array arr. In the for loop, we’re incrementing the pointer by i each time, effectively moving the pointer to the i-th element of the array. We then dereference the pointer using the * operator to get the value of the i-th element, which we print.

The output of this code will be: 1 2 3 4 5.

Code Example 2: Swapping Two Numbers Using Pointers and Pointer Arithmetic

#include <stdio.h>

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int num1 = 5, num2 = 10;
    swap(&num1, &num2);
    printf("num1 = %d, num2 = %d", num1, num2);
    return 0;
}
C

In this code, we’re using pointers to swap two numbers. The swap function takes two pointers as arguments. It dereferences these pointers to get the values they point to, swaps these values, and then assigns the swapped values back to the memory locations pointed to by the pointers. This effectively swaps the values of the two numbers in the main function.

The output of this code will be: num1 = 10, num2 = 5.

Wrapping Up

And there you have it! You now know what pointer arithmetic is, how it works, and how to use it in your C programs. Remember, pointers are a powerful tool in C, and understanding how to use them effectively can make your code more efficient and easier to understand.

Frequently Asked Questions (FAQ)

  1. What is a pointer arithmetic in C?

    Pointer arithmetic involves performing arithmetic operations on pointers, such as incrementing or decrementing a pointer, or adding or subtracting an integer to a pointer.

  2. What is pointer arithmetic with example?

    An example of pointer arithmetic is incrementing a pointer to move it to the next element in an array. For instance, if ptr is a pointer to an integer array, doing ptr++ will move the pointer to the next integer in the array.

  3. What is the purpose of pointer arithmetic?

    The purpose of pointer arithmetic is to allow you to manipulate pointers to traverse and access elements in arrays and other data structures.

  4. Which pointer arithmetic operator can be used in C?

    The pointer arithmetic operators in C are ++ (increment), -- (decrement), + (addition), and - (subtraction).

  1. Understanding Pointers in C
  2. Pointer Arithmetic in C
  3. Pointers with Arrays in C
  4. Pointers with Strings in C
  5. Pointers with Functions in C
  6. C Dynamic Memory Allocation
Scroll to Top