Basic Pointers in C++
Basic Pointer Program in C++.
BeginnerMemory Management ProgramsExample 1 of 11
basic-pointers.cpp
1#include <iostream>2using namespace std;34int main() {5 int num = 10;67 // Declare a pointer8 int* ptr;910 // Store address of num in pointer11 ptr = #1213 cout << "Value of num: " << num << endl;14 cout << "Address of num: " << &num << endl;15 cout << "Value of ptr (address stored): " << ptr << endl;16 cout << "Address of ptr: " << &ptr << endl;17 cout << "Value pointed by ptr: " << *ptr << endl;1819 // Modify value using pointer20 *ptr = 20;21 cout << "\nAfter modifying through pointer:" << endl;22 cout << "Value of num: " << num << endl;23 cout << "Value pointed by ptr: " << *ptr << endl;2425 return 0;26}
Output
Value of num: 10 Address of num: 0x7fff5fbff6ac Value of ptr (address stored): 0x7fff5fbff6ac Address of ptr: 0x7fff5fbff6a0 Value pointed by ptr: 10 After modifying through pointer: Value of num: 20 Value pointed by ptr: 20
What's going on
The program demonstrates basic pointer operations:
Declaring a pointer variable
Getting address of a variable using & operator
Storing address in pointer
Accessing value through pointer using * operator (dereferencing)
Pointers allow you to work with memory addresses directly.
Header File Used
This header provides:
cout for displaying output
cin for taking input from the user
1#include <iostream>
Understanding Pointers
Pointer Concept
:
Variable that stores memory address
Points to location of another variable
Enables indirect access to data
Fundamental for dynamic memory
Memory Address
:
Every variable has a memory address
Address is where data is stored
Pointer stores this address
Can access data through address
Declaring Pointers
Syntax
:
dataType* pointerName;
Example
:
int* ptr; // Pointer to integer
How it works
:
* indicates pointer type
ptr can store address of int variable
Initially contains garbage (uninitialized)
Address Operator (&)
Getting Address
:
int num = 10;
int* ptr = # // & gets address of num
How it works
:
& operator returns memory address
Address stored in pointer variable
Example: &num returns address like 0x7fff5fbff6ac
Dereference Operator (*)
Accessing Value
:
int value = *ptr; // * accesses value at address
How it works
:
* operator dereferences pointer
Accesses value at stored address
Example: *ptr returns value 10 (from num)
Pointer Operations
Declaration and Initialization
:
int num = 10;
int* ptr = #
Accessing Value
:
1cout << *ptr; // Prints 10
Modifying Through Pointer
:
*ptr = 20; // Changes num to 20
Getting Address
:
1cout << ptr; // Prints address2cout << # // Same address
When to Use Pointers
Best For
:
Dynamic memory allocation
Passing parameters by reference
Arrays and strings
Data structures (linked lists, trees)
Function parameters (modify original)
Example Scenarios
:
Allocating memory at runtime
Passing large objects efficiently
Implementing data structures
Working with arrays
Important Considerations
Initialization
:
Always initialize pointers
Uninitialized pointers are dangerous
Initialize to nullptr if not pointing anywhere
Null Pointers
:
nullptr: doesn't point to anything
Always check before dereferencing
Prevents crashes
Dereferencing
:
Only dereference valid pointers
Check for null before use
Undefined behavior if invalid
return 0;
This ends the program successfully.