1. Basics of Pointers
- A pointer is a variable that stores a memory address.
- Pointer definition and initialization:
<span><span>int *p = &a;</span></span> - Pointer operations:
<span><span>++</span></span>,<span><span>--</span></span>,<span><span>+n</span></span>,<span><span>-n</span></span> - The relationship between pointers and arrays: the name of the array is the address of the first element.
Exercise: Swap Two Values

After swap1: x=10, y=20
After swap2: x=20, y=10
2. In-depth Understanding of References
Definition and Essence of References
A reference is an alias for a variable, must be initialized at the time of definition, and once initialized, cannot point to another variable.

Differences Between References and Pointers
| Feature | Reference | Pointer |
|---|---|---|
| Definition | Must be initialized | Can be uninitialized (but not recommended) |
| Null Value | No null references | Can have null pointers (NULL/nullptr) |
| Pointing | Cannot change the reference once initialized | Can change the pointer at any time |
| Memory | Does not occupy extra memory (conceptually) | Occupies memory (stores address) |
| Operation | Used directly, no need for dereferencing | Requires dereference operator<span><span>*</span></span> |
References as Function Parameters (Reference Passing)

Advantages of Reference Passing
- Simplified syntax, no need to use
<span><span>*</span></span>and<span><span>&</span></span>operators - Avoids the overhead of copying in value passing
- Safer than pointers (no null or dangling references)
References as Function Return Values

Note: Do not return a reference to a local variable, as local variables will be destroyed after the function ends.
3. Advanced Pointers
1. Comparison of Pointer Passing and Reference Passing



2. Constant Pointers and Pointer Constants

<span><span>const int *p</span></span>const modifies<span><span>*p</span></span>, meaning the content pointed to cannot change.<span><span>int *const p</span></span>const modifies<span><span>p</span></span>, meaning the pointer itself cannot change.
3. Pointer Arrays and Array Pointers
Distinction Technique: Based on operator precedence
<span><span>int *arr[3]</span></span><span><span>[]</span></span>has higher precedence than<span><span>*</span></span>, so it forms a pointer array first.<span><span>int (*p)[3]</span></span><span><span>()</span></span>changes precedence, forming an array pointer first.
4. Pointers to Functions (Function Pointers)

Summary
- A reference is an alias for a variable, must be initialized, and cannot change its pointing.
- Reference passing is more concise and safer than pointer passing, suitable for most scenarios.
- The difference between constant pointers and pointer constants lies in the object modified by const.
- Pointer arrays store pointers, while array pointers point to arrays.
- Function pointers allow dynamic function calls, increasing program flexibility.
Exercise:Analyze the output of the following function