Pointers
What Are Pointers in C++?
Key Concepts of Pointers
Simple Program Examples
#include <iostream>
int main() {
int x = 10; // A regular integer variable
int* ptr = &x; // A pointer that stores the address of x
// Display the value of x and the address of x
std::cout << "Value of x: " << x << std::endl;
std::cout << "Address of x: " << &x << std::endl;
// Display the value stored in ptr (which is the address of x)
std::cout << "Value stored in ptr (address of x): " << ptr << std::endl;
// Dereference ptr to get the value of x
std::cout << "Value pointed to by ptr: " << *ptr << std::endl;
// Modify the value of x using the pointer
*ptr = 20;
std::cout << "New value of x after modification through pointer: " << x << std::endl;
return 0;
}Common Use Cases for Pointers
Summary
Last updated