C Language
7. Pointers & Memory ManipulationIntermediate12 min read

Pointers in C: Fundamentals & Memory Addresses

The core superpower of C: understanding memory addresses, the address-of operator (&), dereference operator (*), and pointer arithmetic.

Key Takeaways & Core Concepts
  • A pointer is a variable whose value is the physical or virtual memory address of another variable.
  • The address-of operator & retrieves the memory address of an operand.
  • The dereference / indirection operator * accesses the value stored at the address pointed to.
  • Pointer arithmetic is scaled automatically by the compiler based on the data type's byte size.
  • Always initialize pointers; uninitialized pointers (wild pointers) can corrupt random memory.

Anatomy of a Pointer

Every variable occupies a location in RAM. A pointer stores that memory address:

C Code Example
1int x = 42; // An integer holding value 42
2int *ptr = &x; // ptr holds the memory address of x (e.g. 0x7ffd58)
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
2 lines
  • int *ptr declares a pointer to an integer.
  • &x yields the address of x.
  • *ptr (dereferencing) accesses or changes the value inside x.
Two Meanings of the Asterisk (*)

When used in a declaration (`int *ptr`), the asterisk denotes that `ptr` is a pointer variable. When used in an expression (`*ptr = 99`), it dereferences the pointer to access the target memory value.

Pointer Arithmetic & Array Traversal

When you increment a pointer (ptr++), it does NOT simply add 1 to the numeric address. It advances by sizeof(type) bytes! If an int is 4 bytes and ptr points to address 1000, then ptr + 1 evaluates to 1004.

Pointer Operations & Arithmetic Example
1#include <stdio.h>
2
3int main(void) {
4 int val = 100;
5 int *p = &val;
6
7 printf("Direct value of val: %d\n", val);
8 printf("Address of val (&val): %p\n", (void*)&val);
9 printf("Value stored in p (addr): %p\n", (void*)p);
10 printf("Dereferenced (*p): %d\n", *p);
11
12 // Modify original value through pointer
13 *p = 250;
14 printf("New value of val: %d\n\n", val);
15
16 // Pointer arithmetic over an array
17 int scores[] = {10, 20, 30, 40};
18 int *sPtr = scores; // points to scores[0]
19
20 printf("Traversing array with pointer arithmetic:\n");
21 for (int i = 0; i < 4; i++) {
22 printf("Index %d: Value = %d at address %p\n", i, *(sPtr + i), (void*)(sPtr + i));
23 }
24
25 return 0;
26}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
26 lines
Note: *(sPtr + i) is mathematically identical to scores[i]. Notice addresses jump by 4 bytes per integer.
Practice & AssessmentC Programming Complete Course

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 2 correct(0 attempted)

1

What does the dereference operator (*) do when used on a valid pointer?

Intermediate
2

If double *p points to address 2000, and sizeof(double) is 8 bytes, what is p + 3?

Intermediate