Arrays with Pointers

Last Updated: Jan 2026


We can use pointers to access elements of an array, calculate the address of an element, perform pointer arithmetic, and compare pointers to determine if they point to the same element or different elements.

In C, the name of an array acts as a constant pointer to its first element (&array[0]).

Because array elements sit contiguously in memory, you can access elements using either subscript notation (arr[i]) or pointer arithmetic (*(arr + i)).

arr[i]≡∗(arr+i)\text{arr}[i] \equiv *(\text{arr} + i)
int scores[] = {88, 92, 95, 100};

// Both lines print the exact same value (92):
printf("%d\n", scores[1]);      // Subscript indexing
printf("%d\n", *(scores + 1));  // Pointer arithmetic


Pointer Arithmetic Operations

Pointer arithmetic does not work like standard math. Adding 1 to a pointer moves it forward by the size of the data type it points to, not by 1 byte.

  • If p is an int* (4 bytes) pointing to address 1000, p + 1 evaluates to 1004.
  • If p is a char* (1 byte) pointing to 1000, p + 1 evaluates to 1001.
  • If p is a double* (8 bytes) pointing to 1000, p + 1 evaluates to 1008.
int arr[] = {10, 20, 30};
int *p = arr; // Points to arr[0] (e.g., 0x2000)

p++; // Now p points to arr[1] (0x2004)
printf("%d\n", *p); // Output: 20


Traversing an Array with Pointers

#include <stdio.h>

int main(void) {
    int data[] = {5, 10, 15, 20};
    int len = sizeof(data) / sizeof(data[0]);

    int *ptr = data; // Points to data[0]

    for (int i = 0; i < len; i++) {
        printf("Element %d: Value = %d | Address = %p\n", i, *ptr, ptr);
        ptr++; // pointer to the next element
    }

    return 0;
}

Key Difference: While ptr can be incremented (ptr++), the array name itself (data) is a fixed label. You cannot do data++.


Pointer Comparison

You can compare two pointers using relational operators (==, !=, <, >, <=, >=), provided they point to elements within the same array.

int numbers[] = {1, 2, 3, 4, 5};
int *p1 = &numbers[0];
int *p2 = &numbers[3];

if (p1 < p2) {
    printf("p1 points to an earlier memory location than p2\n");
}


Array of Pointers and Pointer to an Array

An array of pointers can be useful when you need to store a collection of pointers to different data structures or objects. While a pointer to an array can be used to access the elements of the array, it can also be used to access the array itself.

int a = 1, b = 2, c = 3;

// Array of pointers : An array containing 3 pointer elements to `int`.
int *ptr_array[3] = {&a, &b, &c};

// Pointer to an array : A single pointer pointing to an entire array of 5 integers.
int numbers[5] = {10, 20, 30, 40, 50};
int (*array_ptr)[5] = &numbers;



Liked This Tutorial

Scan QR Code To Leave A Review Or

Click Here To Leave A Review
Scan QR