Advanced C++ References and Pointers

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

Advanced C++ References and Pointers

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.

Advanced C++ References and Pointers

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)

Advanced C++ References and Pointers

Advantages of Reference Passing

  1. Simplified syntax, no need to use<span><span>*</span></span>and<span><span>&</span></span>operators
  2. Avoids the overhead of copying in value passing
  3. Safer than pointers (no null or dangling references)

References as Function Return Values

Advanced C++ References and Pointers

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

Advanced C++ References and PointersAdvanced C++ References and PointersAdvanced C++ References and Pointers

2. Constant Pointers and Pointer Constants

Advanced C++ References and Pointers

  • <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

Advanced C++ References and PointersDistinction 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)

Advanced C++ References and 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 functionAdvanced C++ References and Pointers

Leave a Comment