#include <iostream>
using namespace std;
int main() {
int num = 10;
// Declare a pointer
int* ptr;
// Store address of num in pointer
ptr = #
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Value of ptr (address stored): " << ptr << endl;
cout << "Address of ptr: " << &ptr << endl;
cout << "Value pointed by ptr: " << *ptr << endl;
// Modify value using pointer
*ptr = 20;
cout << "\nAfter modifying through pointer:" << endl;
cout << "Value of num: " << num << endl;
cout << "Value pointed by ptr: " << *ptr << endl;
return 0;
}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
This program teaches you how to use Basic Pointers in C++. A pointer is a variable that stores the memory address of another variable. Pointers enable indirect access to variables and are fundamental to dynamic memory management, arrays, and advanced C++ programming.
1. What This Program Does
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.
2. Header File Used
#include <iostream>
This header provides:
- cout for displaying output
- cin for taking input from the user
3. 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
4. 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)
5. 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
6. 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)
7. Pointer Operations
Declaration and Initialization:
int num = 10; int* ptr = #
Accessing Value:
cout << *ptr; // Prints 10
Modifying Through Pointer:
*ptr = 20; // Changes num to 20
Getting Address:
cout << ptr; // Prints address cout << # // Same address
8. 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
9. 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
10. return 0;
This ends the program successfully.
Summary
- Pointer: variable that stores memory address of another variable.
- & operator: gets address of a variable.
-
- operator: dereferences pointer to access value.
- Pointers enable indirect access to variables.
- Fundamental for dynamic memory management.
- Understanding pointers is essential for advanced C++ programming.
- Always initialize pointers and check for null before dereferencing.
This program is fundamental for learning memory management, understanding addresses, and preparing for dynamic memory allocation and advanced data structures in C++ programs.