373 字
2 分钟
Reference and Pointer
Reference vs Pointer
| Aspect | Reference | Pointer |
|---|---|---|
| Definition | An alias (another name) for an existing variable | A variable that stores the address of another variable |
| Initialization | Must be initialized when declared | Can be declared without initialization |
| Null value | Cannot be null | Can be null (nullptr / NULL) |
| Reassignment | Cannot be changed to refer to another object | Can point to a different object |
| Syntax usage | Used directly like a normal variable | Requires dereferencing (*) to access the value |
| Memory | Does not require separate memory | Requires its own memory to store an address |
| Safety | Safer, fewer chances of misuse | Less safe, can cause dangling or null pointer issues |
| Arithmetic | Not allowed | Pointer arithmetic is allowed |
| Use case | When you always refer to the same object | When you need optional references or dynamic allocation |
Simple Example
int x = 10;
int& ref = x; // referenceint* ptr = &x; // pointer
ref = 20; // x becomes 20*ptr = 30; // x becomes 30One-Line Summary (Exam-Friendly)
- A reference is a constant alias to a variable and cannot be null or reassigned.
- A pointer stores a memory address, can be null, and can be reassigned.
When to Use a Reference Parameter
1. To modify the original argument
- Changes inside the function affect the caller’s variable.
void increment(int& x) { x++;}📌 Use a reference when the parameter must exist and must not be null.
2. To avoid copying large objects
- More efficient than pass-by-value.
void print(const string& s) { cout << s;}📌 Use a const reference when you don’t want to modify the object.
3. When the object should not change identity
- The function should always refer to the same object.
When to Use a Pointer Parameter
1. When the parameter can be optional (nullable)
- A pointer can be
nullptr, a reference cannot.
void print(int* p) { if (p != nullptr) cout << *p;}2. When the function needs to work with arrays or dynamic memory
- Common in low-level code.
void fillArray(int* arr, int size) { for (int i = 0; i < size; i++) arr[i] = i;}3. When you need to change what the pointer points to
- (e.g., allocate or redirect memory)
void allocate(int*& p) { p = new int(10);}Reference vs Pointer (Decision Guide)
| Situation | Use Reference | Use Pointer |
|---|---|---|
| Must not be null | ✅ | ❌ |
| Optional parameter | ❌ | ✅ |
| Avoid copying | ✅ | ✅ |
| Modify original object | ✅ | ✅ |
| Change what is referenced | ❌ | ✅ |
| Safer & cleaner code | ✅ | ❌ |
Short Exam Answer
- Use a reference when the parameter must always refer to a valid object and you want safer, cleaner syntax.
- Use a pointer when the parameter can be null, optional, or needs to be reassigned.
Reference and Pointer
https://mizuki.anka2.top/posts/l5-cpp-week12-lecture/reference-and-pointer/ 部分信息可能已经过时









