Basic Pointers

Basic Pointer Program in C++

BeginnerTopic: Memory Management Programs
Back

C++ Basic Pointers Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int num = 10;
    
    // Declare a pointer
    int* ptr;
    
    // Store address of num in pointer
    ptr = &num;
    
    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

Understanding Basic Pointers

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

This header provides:

cout for displaying output
cin for taking input from the user

---

#include <iostream>

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 = &num; // & 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 = &num;

Accessing Value

:

cout << *ptr;  // Prints 10

Modifying Through Pointer

:

*ptr = 20; // Changes num to 20

Getting Address

:

---

cout << ptr;  // Prints address
cout << &num;  // 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.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Basic Pointers

This C++ program is part of the "Memory Management Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents