Pointers and References

📘 C++ 👁 31 views 📅 Dec 22, 2025
⏱ Estimated reading time: 2 min

A pointer is a variable that stores the memory address of another variable.


Declaration of Pointer

data_type *pointer_name;

Example

int x = 10; int *ptr = &x;
  • &x → address of variable x

  • *ptr → value at the address stored in ptr


Accessing Value Using Pointer

cout << *ptr; // Output: 10

Pointer Arithmetic

Pointers can be incremented or decremented.

ptr++; ptr--;

Null Pointer

A pointer that does not point to any valid memory.

int *ptr = NULL; // or int *ptr = nullptr;

Advantages of Pointers

  • Dynamic memory management

  • Efficient array handling

  • Used in data structures (linked lists, trees)


References in C++

A reference is an alias (alternative name) for an existing variable.


Declaration of Reference

data_type &reference_name = variable_name;

Example

int x = 10; int &ref = x;

Accessing Reference

ref = 20; cout << x>// Output: 20

Key Differences Between Pointer and Reference

PointerReference
Stores addressAlias of variable
Can be reassignedCannot be reassigned
Can be NULLCannot be NULL
Uses * to access valueAccessed like normal variable

Call by Reference Example

void swap(int &a, int &b) { int temp = a; a = b; b = temp; }

Key Points

  • Pointers store addresses

  • References are safer and easier to use

  • Pointers support dynamic memory

  • References are commonly used in functions


Conclusion

Pointers and references are powerful features in C++ that allow efficient memory access and manipulation.



🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes