Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Reference vs Pointers | References Fundamentals
C++ Pointers and References

Reference vs PointersReference vs Pointers

References and pointers provide indirect access to variable values. but they have some key differences, as well as pros and cons.

Syntax

  • Pointers are declared using the * symbol;
  • References are declared using the & symbol.

Initialization

You can't declare an empty reference. But you can declare an empty pointer with nullptr.

Good
int* p_variable = nullptr
Error
int& ref_variable;

Reassignment

References can't be re-assigned to refer to a different object.

cpp

main.cpp

Pointers can be re-assigned to point to different memory locations

cpp

main.cpp

Accessing the value

To access the value pointed by a pointer, you use the dereference operator *. For references, you don't need to use any special operator; you simply use the reference variable directly.

Pointer
*(p_variable)
Reference
ref_variable

Both pointers and references are crucial in memory management, contributing to the flexibility of programs. Their usage depends on the context.

Все було зрозуміло?

Секція 3. Розділ 4
course content

Зміст курсу

C++ Pointers and References

Reference vs PointersReference vs Pointers

References and pointers provide indirect access to variable values. but they have some key differences, as well as pros and cons.

Syntax

  • Pointers are declared using the * symbol;
  • References are declared using the & symbol.

Initialization

You can't declare an empty reference. But you can declare an empty pointer with nullptr.

Good
int* p_variable = nullptr
Error
int& ref_variable;

Reassignment

References can't be re-assigned to refer to a different object.

cpp

main.cpp

Pointers can be re-assigned to point to different memory locations

cpp

main.cpp

Accessing the value

To access the value pointed by a pointer, you use the dereference operator *. For references, you don't need to use any special operator; you simply use the reference variable directly.

Pointer
*(p_variable)
Reference
ref_variable

Both pointers and references are crucial in memory management, contributing to the flexibility of programs. Their usage depends on the context.

Все було зрозуміло?

Секція 3. Розділ 4
some-alt