Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Basic Pointers in C++

Learn C++ pointers from scratch: store an address with &, read and change a value through a pointer with *, and see why ptr and &ptr print different addresses.

BeginnerMemory Management ProgramsExample 1 of 11
basic-pointers.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num = 10;
6
7 // Declare a pointer
8 int* ptr;
9
10 // Store address of num in pointer
11 ptr = &num;
12
13 cout << "Value of num: " << num << endl;
14 cout << "Address of num: " << &num << endl;
15 cout << "Value of ptr (address stored): " << ptr << endl;
16 cout << "Address of ptr: " << &ptr << endl;
17 cout << "Value pointed by ptr: " << *ptr << endl;
18
19 // Modify value using pointer
20 *ptr = 20;
21 cout << "\nAfter modifying through pointer:" << endl;
22 cout << "Value of num: " << num << endl;
23 cout << "Value pointed by ptr: " << *ptr << endl;
24
25 return 0;
26}

Sample Output

Value pointed by ptr: 10

Dereferencing ptr reads num.

Value of num: 20

Changed through *ptr = 20.

In this one

  • What a pointer stores
  • How & gets an address and * follows one
  • Why changing *ptr changes the original variable
  • The two meanings of * in declarations and expressions

How it works

  1. 1

    num = 10.

  2. 2

    ptr = &num stores num's address.

  3. 3

    Print num, &num, ptr, &ptr, and *ptr.

  4. 4

    *ptr = 20 changes num through the pointer.

  5. 5

    Print num and *ptr again: both 20.

Edge cases

Uninitialized pointer

Remove ptr = &num; and print *ptr.

ptr holds garbage, so dereferencing it is undefined behavior: a crash or a random value. Initialize pointers, at least to nullptr.

Different addresses on each run

Run the program twice.

Operating systems randomize stack addresses for security, so the hex values change. Only the relationships (ptr equals &num) stay the same.

What's going on

A pointer is a variable that stores a memory address. This program makes one, points it at an int, reads the value through it, and then changes the int through it.

int num = 10; is a plain variable somewhere in memory.

int* ptr; declares a pointer to an int. Until it is given an address, it holds garbage, and reading through it would be undefined behavior.

ptr = &num; stores the address of num in ptr. The & here is the address-of operator.

The prints show four different things. num is 10. &num is where num lives (a hex address like 0x7fff5fbff6ac). ptr prints the same address, because that is what it holds. &ptr is a different address: the pointer is a variable too, so it lives somewhere of its own. *ptr is 10: the * in an expression means "go to the address and read what is there", called dereferencing.

*ptr = 20; writes through the pointer. Nothing assigns num directly, yet num now prints 20, because ptr and num refer to the same memory.

The * has two jobs, which confuses everyone at first. In a declaration (int* ptr) it means "this is a pointer". In an expression (*ptr) it means "follow the pointer".

Your addresses will differ from the sample output. They change from machine to machine and often from run to run.

The only header is <iostream>. It gives you cout for displaying output and cin for taking input from the user.

Understanding Pointers

Pointer Concept:

Variable that stores memory address
Points to location of another variable
Enables indirect access to data
Fundamental for dynamic memory

Memory Address:

Every variable has a memory address
Address is where data is stored
Pointer stores this address
Can access data through address

Declaring Pointers

Syntax:

dataType* pointerName;

Example:

int* ptr; // Pointer to integer

What happens here:

* indicates pointer type
ptr can store address of int variable
Initially contains garbage (uninitialized)

Address Operator (&)

Getting Address:

int num = 10;

int* ptr = &num; // & gets address of num

What happens here:

& operator returns memory address
Address stored in pointer variable
Example: &num returns address like 0x7fff5fbff6ac

Dereference Operator (*)

Accessing Value:

int value = *ptr; // * accesses value at address

What happens here:

* operator dereferences pointer
Accesses value at stored address
Example: *ptr returns value 10 (from num)

Pointer Operations

Declaration and Initialization:

int num = 10;

int* ptr = &num;

Accessing Value:

cout << *ptr; // Prints 10

Modifying Through Pointer:

*ptr = 20; // Changes num to 20

Getting Address:

cout << ptr; // Prints address

cout << &num; // Same address

When to Use Pointers

Best For:

Dynamic memory allocation
Passing parameters by reference
Arrays and strings
Data structures (linked lists, trees)
Function parameters (modify original)

Example Scenarios:

Allocating memory at runtime
Passing large objects efficiently
Implementing data structures
Working with arrays

Important Considerations

Initialization:

Always initialize pointers
Uninitialized pointers are dangerous
Initialize to nullptr if not pointing anywhere

Null Pointers:

nullptr: doesn't point to anything
Always check before dereferencing
Prevents crashes

Dereferencing:

Only dereference valid pointers
Check for null before use
Undefined behavior if invalid

return 0;

This ends the program successfully.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

After int x = 5; int* p = &x; *p = 9; what is x?

Pick an answer to check it.

Question 2

What does &ptr give?

Pick an answer to check it.

Questions people ask

What is the size of a pointer?

Usually 8 bytes on 64-bit systems, no matter what type it points to.

Is int* p or int *p correct?

Both. The space is just style. Watch out for int* a, b; which makes only a a pointer.

What is a reference?

A reference (int& r = num) is another name for num. It cannot be null or reseated.