JavaScript Error

You currently have JavaScript disabled on your web browser.

This website uses JavaScript, and This web page needs JavaScript activated to work correctly.

Please active JavaScript on your web browser and then refresh this web page.



C++ Pointers



In C++ a Pointer is a variable that holds the memory address of another variable. So instead of storing data directly, it stores the memory location of where to find the data.


Pointers vs References

In C++, pointers and references are commonly used frequently in software development, however they are not the same thing and there are differences between the two:

Feature Pointer Reference
Must be initialized?NoYes
Can be reassigned?YesNo
Can be null?YesNo
SyntaxMore flexibleMore readable
Used in dynamic memoryYesN/A

Common Pointer Operations

OperationSyntaxMeaning
Get address&xWhere x lives in memory
Declare pointerint* ptrptr will point to an int
Assign addressptr = &xptr now knows where x is
Dereference*ptrGets or sets the value at ptr
Null pointerint* ptr = nullptrPointer points to nothing (safe default)

Pointer Example

 
int x = 42;
int* ptr = &x;  // 'ptr' now stores the memory address of 'x'

// Output the value of x : 42
cout << x << "\n";

// Output the memory address of x : 0x6egfc9
cout << &x << "\n";

// Output the memory address of x with the pointer : 0x6egfc9
cout << ptr << "\n";

Let's break down int* ptr = &x;:




Final Thoughts

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.


(0) pollYesResult
(0) pollNoResult



 
     About   |   Contact Us   |   Privacy   |   Terms & Conditions   |   © 2025 - T&J Divisions, LLC, All Rights Reserved