

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Memory
incomplete
2: What Is an Address?
incomplete
3: Virtual Memory
incomplete
4: Pointers
incomplete
5: Why Pointers?
incomplete
6: Pointer Basics
incomplete
7: Pointers to Structs
incomplete
8: C Arrays
incomplete
9: Arrays As Pointers in C
incomplete
10: Multibyte Arrays
incomplete
11: Array Casting
incomplete
12: Pointer Size
incomplete
13: Arrays Decay to Pointers
incomplete
14: C Strings
incomplete
15: C String Library
incomplete
16: Forward Declaration
incomplete
17: Mutual Structs
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
In C, arrays and pointers are closely related. An array name acts as a pointer to the first element of the array. That means array indexing and pointer arithmetic can be used interchangeably to access array elements. Let's go through this step-by-step to understand how this works.
Array Declaration:
int numbers[5] = {1, 2, 3, 4, 5};
Here, numbers is an array of 5 integers.
Array as Pointer:
The name numbers acts as a pointer to the first element of the array.
int *numbers_ptr = numbers;
numbers_ptr is a pointer to the same place as numbers.
Accessing Elements via Indexing:
// Access the third element (index 2)
int value = numbers[2];
Which is the same as:
int value = *(numbers + 2);
Here, numbers + 2 computes the address of the third element, and * dereferences it to get the value.
Pointer Arithmetic:
When you add an integer to a pointer, the resulting pointer is offset by that integer times the size of the data type.
int *p = numbers + 2; // p points to the third element
int value = *p; // value is 3
Let's assume numbers is stored starting at memory address 0x1000. An integer is typically 4 bytes in C. Here's how the array elements are laid out in memory:
| Address | Element | Value |
|---|---|---|
| 0x1000 | numbers[0] | 1 |
| 0x1004 | numbers[1] | 2 |
| 0x1008 | numbers[2] | 3 |
| 0x100C | numbers[3] | 4 |
| 0x1010 | numbers[4] | 5 |
Try it out! Change the base address, operator, and offset to see how pointer arithmetic selects different memory cells:
Interactive example available with JavaScript enabled.
numbers + 0 or &numbers[0] points to 0x1000numbers + 1 or &numbers[1] points to 0x1004numbers + 2 or &numbers[2] points to 0x1008numbers + 3 or &numbers[3] points to 0x100Cnumbers + 4 or &numbers[4] points to 0x1010#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
// Accessing elements using array indexing
printf("numbers[2] = %d\n", numbers[2]); // Output: 3
// Accessing elements using pointers
printf("*(numbers + 2) = %d\n", *(numbers + 2)); // Output: 3
// Pointer arithmetic
int *ptr = numbers;
printf("Pointer ptr points to numbers[0]: %d\n", *ptr); // Output: 1
ptr += 2;
printf("Pointer ptr points to numbers[2]: %d\n", *ptr); // Output: 3
return 0;
}