0
0
Cprogramming~5 mins

Pointers and arrays in C

Choose your learning style9 modes available
Introduction

Pointers and arrays let you work directly with memory locations. This helps you handle data efficiently and understand how computers store information.

When you want to access or change elements in a list quickly.
When you need to pass large arrays to functions without copying all data.
When working with strings, which are arrays of characters.
When you want to create dynamic data structures like linked lists.
When you want to understand how memory works in your program.
Syntax
C
/* Define an array and a pointer to its first element */
int numbers[5] = {10, 20, 30, 40, 50};
int *pointer_to_numbers = numbers;

/* Access elements using pointer arithmetic */
int first = *pointer_to_numbers;       // 10
int second = *(pointer_to_numbers + 1); // 20

An array name like numbers acts like a pointer to its first element.

You can move the pointer to access other elements by adding an index.

Examples
Basic example showing how to access array elements using a pointer.
C
int values[3] = {5, 10, 15};
int *ptr = values;

// Access first element
int first_value = *ptr; // 5

// Access second element
int second_value = *(ptr + 1); // 10
Edge case: empty array has no elements, so pointer arithmetic should be avoided.
C
int *ptr = NULL;

// Accessing elements here is unsafe because array is empty.
Edge case: array with one element, pointer can access only that element safely.
C
int single_element[1] = {42};
int *ptr = single_element;

int value = *ptr; // 42

// Moving pointer beyond this is unsafe.
Pointer pointing to the last element of the array.
C
int data[4] = {1, 2, 3, 4};
int *ptr = data + 3; // points to last element

int last_value = *ptr; // 4
Sample Program

This program shows how to access and modify array elements using pointers. It prints the array elements first using normal array indexing, then using pointer arithmetic. Finally, it changes the third element through the pointer and prints the updated array.

C
#include <stdio.h>

int main() {
    int numbers[5] = {10, 20, 30, 40, 50};
    int *pointer_to_numbers = numbers; // points to first element

    printf("Array elements using array syntax:\n");
    for (int i = 0; i < 5; i++) {
        printf("numbers[%d] = %d\n", i, numbers[i]);
    }

    printf("\nArray elements using pointer arithmetic:\n");
    for (int i = 0; i < 5; i++) {
        printf("*(pointer_to_numbers + %d) = %d\n", i, *(pointer_to_numbers + i));
    }

    // Change third element using pointer
    *(pointer_to_numbers + 2) = 100;

    printf("\nAfter changing third element via pointer:\n");
    for (int i = 0; i < 5; i++) {
        printf("numbers[%d] = %d\n", i, numbers[i]);
    }

    return 0;
}
OutputSuccess
Important Notes

Time complexity to access any element by pointer or array index is O(1).

Space complexity is the same for arrays and pointers; pointers just hold an address.

Common mistake: moving pointer beyond array bounds causes undefined behavior.

Use pointers when you want efficient access or to pass arrays to functions without copying.

Summary

Pointers can point to the first element of an array.

You can access array elements by moving the pointer with arithmetic.

Be careful not to go outside the array limits with pointers.