By David Marín
A pointer is a variable that stores a memory address. Instead of holding a value directly, it points to where the value lives in RAM.
Think of a pointer like a house address instead of the house itself.
When you write:
int x = 10;
Memory might look like:
Address Value 0x7ffe0010 10
int x = 10; int *p = &x;
int *p → p is a pointer to an int&x → the memory address of x
printf("%d", *p);
*p means: go to the address stored in p and read the value.
x -----> 10 p -----> &x
Or more literally:
p -> 0x7ffe0010 -> 10
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d", *(p + 1)); // prints 20
Memory layout:
p -> arr[0] p + 1 -> arr[1] p + 2 -> arr[2]
int *p = NULL; *p = 10; // crash
int *p;
{
int x = 10;
p = &x;
} // x destroyed here
Now p points to invalid memory.
int *p = malloc(sizeof(int)); // forgot free(p);
#include <memory> std::unique_ptr<int> p = std::make_unique<int>(10);
No manual free required.
int x = 10; int &r = x; // reference int *p = &x; // pointer
| Feature | Pointer | Reference |
|---|---|---|
| Can be null | Yes | No |
| Can change target | Yes | No |
| Syntax | *p | r |
Pointers expose raw memory. Mistakes can cause crashes, security vulnerabilities, and undefined behavior. Many exploits (buffer overflows, use-after-free) come from pointer misuse.
Visual explanation of pointers in C/C++:
Not all languages treat pointers the same. Understanding this difference is key to understanding why C and C++ feel “low-level” and C# feels “high-level”.
C is a systems programming language, and pointers are everywhere. You use pointers for:
malloc, free)In C, you cannot escape pointers. Even arrays decay into pointers internally.
C++ inherited pointers from C and still uses them for low-level programming, performance, and hardware interaction. However, C++ also adds safer abstractions:
int &r)std::unique_ptr, std::shared_ptr)Modern C++ encourages avoiding raw pointers unless you truly need low-level control.
C# is a managed language running on the .NET runtime. Memory is handled automatically by the Garbage Collector (GC), so you usually never see pointers.
However, pointers do exist in C# in special cases:
unsafe code blocksMost C# developers never touch pointers, but the runtime itself is built on them internally.
Pointers are what separate high-level programming from systems programming. If you understand pointers, you understand how computers really work under the hood.