C Programming Tutorial 0/65 lessons ~6 min read Lesson 30

    Pointer Arithmetic

    Pointer arithmetic moves pointers by sizeof(type) — p+1 points to the next element in an array.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Pointer arithmetic moves pointers by sizeof(type) — p+1 points to the next element in an array.

    Understanding the topic

    Increment p++ moves to next int element.

    Difference p2 - p1 gives element count between pointers.

    Bounds Only valid within same array object.

    • Increment — p++ moves to next int element.
    • Difference — p2 - p1 gives element count between pointers.
    • Bounds — Only valid within same array object.

    Step-by-step explanation

    1. Increment — p++ moves to next int element.
    2. Difference — p2 - p1 gives element count between pointers.
    3. Bounds — Only valid within same array object.

    Informative example

    Example program:

    c
    #include <stdio.h>
    int main(void) {
    int arr[] = {10, 20, 30};
    int *p = arr;
    printf("%d %d\n", *p, *(p + 2));
    return 0;
    }

    Output

    10 30

    Execution workflow

    1Pointer Arithmetic — step by step
    1 / 3

    Increment

    p++ moves to next int element.

    Best practices

    • Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
    • Give every variable a defined value before it is read.
    • Stay inside array bounds — C will not stop you from over-running a buffer.

    Common mistakes

    • Reading uninitialized storage — behavior is undefined.
    • Dismissing compiler warnings instead of fixing root causes.
    • Ignoring NULL returns from malloc, fopen, and similar APIs.

    Hands-on exercise

    Practice problems:

    • Traverse array with pointer only
    • Print string with char*

    Summary

    Pointer Arithmetic in C — Moving by element size within an array object.

    Ready to mark this lesson complete?Track your journey across the entire course.