capypad
0 day streak
cpp / beginner
Snippet

Pointers: Accessing Memory Addresses

A pointer is a variable that stores the memory address of another variable. The asterisk (*) indicates a pointer type, and the ampersand (&) gets the address of a variable. Dereferencing a pointer with * retrieves the value at that address.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
 
int main() {
int score = 42;
int* ptr = &score;
cout << "Value: " << score << endl;
cout << "Address: " << ptr << endl;
cout << "Dereference: " << *ptr << endl;
return 0;
}
Breakdown
1
int* ptr = &score;
Declares a pointer that stores the address of an integer variable
2
*ptr
Dereferences the pointer to access the actual value stored at that address