Home > Backend Development > C++ > What is the difference between pointers and references in C++?

What is the difference between pointers and references in C++?

WBOY
Release: 2024-06-02 16:47:01
Original
358 people have browsed it

Pointers and references are both tools for dealing with memory locations, but there are differences: 1. Pointers store variable addresses, and references point directly to variables. 2. Pointers access variables indirectly and references directly access them. 3. The pointer can point to null, and the reference must point to a valid variable. 4. Using pointers to exchange variable values ​​requires dereference, but not reference.

指针和引用在 C++ 中有何区别?

The difference between pointers and references in C++

Pointers and references are powerful tools for handling memory locations in C++, but There are some key differences between them.

Definition

  • A pointer is a variable that stores the address (memory location) of another variable.
  • Reference is an alias that points directly to another variable.

Syntax

  • Pointer: int *ptr;
  • Quote: int &ref;

Indirect access

  • Pointer indirect access to the target variable:*ptr
  • Reference directly accesses the target variable: ref

Life cycle

  • Pointer: can point to another variable or to null (nullptr).
  • Reference: Must always point to a valid variable.

Dereference

  • The pointer must be dereferenced to obtain the address of the target variable: *ptr
  • The reference does not need to be dereferenced because it directly accesses the target variable.

Practical case: exchanging the values ​​of two variables

The following is a practical case of exchanging the values ​​of two integer variables using pointers and references:

  • Pointer:

    void swapPtr(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
    }
    
    int main() {
    int a = 5, b = 10;
    swapPtr(&a, &b);
    // a 现在是 10,b 现在是 5
    }
    Copy after login
  • Reference:

    void swapRef(int &x, int &y) {
    int temp = x;
    x = y;
    y = temp;
    }
    
    int main() {
    int a = 5, b = 10;
    swapRef(a, b);
    // a 现在是 10,b 现在是 5
    }
    Copy after login

Conclusion

  • Pointers provide indirect access to memory locations and can point to null.
  • References provide direct access to variables and must always point to a valid variable.
  • Pointers are a better choice when indirect access to memory locations (such as arrays, structures) is required.
  • For situations where direct access to variables (such as function parameters) is required, references are a better choice.

The above is the detailed content of What is the difference between pointers and references in C++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template