Tsallaka zuwa abun ciki
Justice Tech School

Pointers and References

A pointer stores an address: & gets it and * reads the value. A reference is another name for an existing variable and cannot be null. Prefer references and smart pointers, and use raw pointers carefully.

Example
int x = 10;
int* p = &x;
*p = 25;
cout << x << endl;
int& ref = x;
ref = 99;
cout << x << endl;
int* none = nullptr;
cout << (none == nullptr) << endl;

Result

25
99
1

Exercise

Read the value a pointer points to.

int v = p;

Quiz Test

What is a reference?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.