Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Scope C Function Declaration C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Nested Structures C Structs & Pointers C Unions C typedef

C Enums

C Enums

C Memory

C Memory Management

C Errors

C Errors C Debugging C NULL C Error Handling C Input Validation

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers

C Projects

C Projects

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Compiler C Syllabus C Study Plan C Certificate

C Pointer Arithmetic


Pointer Arithmetic

Pointer arithmetic means changing the value of a pointer to make it point to a different element in memory.

Like we saw on the previous page, array elements are stored next to each other in memory. So if a pointer points to one element, adding 1 moves it to the next one:

Example

int myNumbers[4] = {25, 50, 75, 100};
int *p = myNumbers;  // points to myNumbers[0]

printf("%d\n", *p);       // 25
printf("%d\n", *(p + 1)); // 50
printf("%d\n", *(p + 2)); // 75
printf("%d\n", *(p + 3)); // 100

Try it Yourself »


Increment and Decrement

You can move a pointer with ++ and -- (and with += / -=):

Example

int myNumbers[3] = {10, 20, 30};
int *p = myNumbers;  // myNumbers[0]

printf("%d\n", *p); // 10
p++;           // move to myNumbers[1]
printf("%d\n", *p); // 20
p--;           // back to myNumbers[0]
printf("%d\n", *p); // 10

p += 2;        // jump to myNumbers[2]
printf("%d\n", *p); // 30

Try it Yourself »

This shows that you can move a pointer just like a counter: each time you use p++ or p--, it moves to the next or previous element in the array.


Pointer Subtraction (Distance)

You can subtract two pointers that point to elements in the same array to find out how many elements are between them:

Example

int myNumbers[5] = {10, 20, 30, 40, 50};
int *start = &myNumbers[1]; // points to 20
int *end = &myNumbers[4];   // points to 50

printf("%ld\n", end - start); // 3 elements apart

Try it Yourself »

Here:

  • start points to the second element (20).
  • end points to the fifth element (50).
  • end - start gives the number of elements between them: 3.

Note: Pointer subtraction only works when both pointers point into the same array. The result is measured in elements, not bytes.


Pointer Arithmetic Depends on Type

Not all pointers move the same way.

When you add 1 to a pointer, it moves forward by the size of the thing it points to - not just by 1 byte.

For example:

  • An int* pointer moves by the size of an integer (usually 4 bytes).
  • A char* pointer moves by the size of a character (1 byte).

So if both pointers start at memory address 1000:

  • int*p + 1 would move to address 1004
  • char*p + 1 would move to address 1001

This shows that pointer movement depends on the data type it points to - not on the number you add:

Example

int myNumbers[2] = {1, 2};
char letters[] = "Hi"; // 'H', 'i', '\0'

int *pi = myNumbers; // int pointer
char *pc = letters; // char pointer

printf("%p\n", (void*)pi);
printf("%p\n", (void*)(pi + 1)); // moves by sizeof(int) (4 bytes)
printf("%p\n", (void*)(pi + 2)); // moves by sizeof(int) (4 bytes)

printf("%p\n", (void*)pc);
printf("%p\n", (void*)(pc + 1)); // moves by 1 byte

Try it Yourself »


Looping with Pointers

In the previous chapter, you learned how to loop through an array using *(ptr + i).

Now let's look at another way - by moving the pointer itself inside the loop. Each time the pointer is increased (p++), it moves to the next element in memory:

Example

int myNumbers[4] = {25, 50, 75, 100};
int *p = myNumbers;    // start of array

for (int i = 0; i < 4; i++) {
  printf("%d\n", *p);
  p++; // move to next element
}

Try it Yourself »

Here's what happens in each loop:

  • *p gives the current element value.
  • p++ moves the pointer to the next element in the array.
  • No array index (i) is needed - the pointer keeps track of the position.

Tip: This way of looping is common when working directly with memory, since the pointer itself moves through the array instead of using an index number.


Common Mistakes to Avoid

  • Using the wrong type: Remember that pointer movement depends on its type. An int* moves in 4-byte steps, but a char* moves 1 byte at a time. Mixing them up will point to the wrong memory locations.
  • Uninitialized pointers: Always make sure a pointer is pointing to something real before you use it. Using a pointer that doesn't point anywhere can crash your program.
  • Going out of bounds: Never move a pointer past the end of an array or before it starts. The only safe "outside" position is one step past the end, and that's only for comparing pointers - not for accessing values.

And be careful; pointers must be handled with care, since it is possible to damage data stored in other memory addresses.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.