Ad space (banner)
Lesson 9 / 20

Pointer Basics

This lesson covers the basics of pointers, one of C++'s defining features. It's for anyone searching "C++ pointer basics."

A pointer is a variable that holds "where in memory another variable is stored" (its address). Declare one with type* variableName, get an address with &variableName, and access the value it points to with *pointer.

The example gets the address of int num = 10; with int* ptr = #, and displays its contents with *ptr.

A common early mistake is confusing & (getting an address) with * (getting the value it points to). It helps to picture a pointer as "a note holding a variable's address" โ€” that makes the difference between the address and the actual value easier to grasp.

In real projects, pointers let you pass large data to a function without copying it, making memory usage far more efficient. This tends to be the first real hurdle in learning C++, but it's a genuinely important concept.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C++ directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)