Pointers in C++ | Class 12 Computer Science Notes
Chapter 7 - Pointers in C++
Contents
Pointers in C++
Pointers are one of the most important concepts in C++. They are used when a program needs to work directly with the memory of a computer. At first, pointers may seem confusing because they involve addresses and dereferencing. But once you understand the basic idea of how variables are stored in memory, pointers become much easier.
This chapter explains Pointers in C++ for Class 12 Computer Science in simple language, with examples, diagrams, programs, important differences.
Introduction to Pointers
Whenever we declare a variable in a C++ program, some space is reserved for that variable in the computer’s memory.
For example:
int marks = 85;
Here, marks stores the value 85. The variable is stored at a particular location in memory, and that location has a unique memory address.
We can obtain the address of a variable using the address-of operator (&).
cout << &marks;
This will display the memory address where marks is stored.
A pointer is a special variable that stores the address of another variable.
Simple idea
Think of a variable as a house and its memory address as the house’s address.
- Variable → House
- Value stored in variable → Things inside the house
- Memory address → Address of the house
- Pointer → A paper on which the house address is written
Definition of Pointer
A pointer is a variable that stores the memory address of another variable.
For example:
int marks = 85;
int *p;
p = &marks;
Here:
- marks is an integer variable.
- &marks gives the address of marks.
- p is a pointer to an integer.
- p stores the address of marks.
So, we can say that p points to marks.
Why Do We Need Pointers?
Pointers are useful because they allow a program to work with memory addresses directly.
Pointers are commonly used for:
- Accessing the address of a variable.
- Accessing a value indirectly.
- Working efficiently with arrays.
- Passing addresses to functions.
- Modifying variables through functions.
- Working with dynamic memory.
- Implementing data structures such as linked lists.
Declaring a Pointer
A pointer must be declared before it can be used. A pointer is declared by placing an asterisk (*) before the pointer variable name
Syntax for declaring a pointer variable is:
data_type *pointer_name;
Here, data type of a pointer tells the compiler what type of data is stored at the address to which the pointer points. It can be int, float, char, etc.
pointer_name : this is the name of the pointer variable
Examples
int *p;
float *q;
char *ch;
Here:
- p is a pointer to an integer.
- q is a pointer to a float.
- ch is a pointer to a character.
Initializing a Pointer
After declaring a pointer, we can initialize it .A pointer is initialized with the address of an existing variable.
int x = 25;
int *p;
p = &x;
Here:
- Variable x stores the value 25.
- &x gives the address of x.
- p stores the address of x.
It can also be written in a single statement:
int x = 25;
int *p = &x;
Now since, p contains the address of x.
We can print the address using:
cout << p;
We can print the value stored at that address using the dereference operator (*):
cout << *p;
Output:
25
Remember
p → address stored in p
*p → value available at that address
This distinction is very important when working with pointers.
Address-of Operator &
The & operator is called the address-of operator. It returns the memory address of a variable.
When a variable is declared, memory is allocated to it. Each memory location has a unique address. We can obtain this address using the address-of operator &.
Syntax
&variable_name
Example
int x = 50;
cout << &x;
Here:
- x refers to the value stored in the variable
- &x gives the memory address of the variable
The output will be a memory address. The exact address can be different each time the program is executed.
Important
The address is assigned by the system and can change
Understanding * in Pointer Declaration
The symbol * is used in two ways.
- To declare a pointer variable
- to access the value stored at the address held by the pointer
Using the symbol * to declare a pointer variable
While declaring a pointer:
int *p;
it indicates that p is an integer pointer
It means:
p can store the address of an integer variable.
Dereference Operator *
When used with an already declared pointer:
cout << *p;
it is called the dereference operator.
Dereferencing a pointer means accessing the value stored at the memory address held by the pointer.
Example
int x = 25;
int *p = &x;
cout << *p;
Output:
25
We can also change the value of x through the pointer.
*p = 40;
Now x becomes 40.
Example of a Pointer
#include <iostream>
using namespace std;
int main()
{
int x = 25;
int *p = &x;
cout << “Value of x = ” << x << endl;
cout << “Address of x = ” << &x << endl;
cout << “Value stored in p = ” << p << endl;
cout << “Value pointed to by p = ” << *p << endl;
return 0;
}
The address displayed by the program may be different each time the program runs
Concept of memory allocation
The process of allocating some area in the main memory to a program is called memory allocation. This can be done in 2 ways:
- Static memory allocation – memory is allocated during compilation
- Dynamic memory allocation – memory is allocated at runtime.
Dynamic Memory Allocation
Normally, memory for variables is allocated automatically when variables are declared.
For example:
int x;
However, sometimes a program needs to allocate memory while it is running. This is called dynamic memory allocation.
C++ provides the new operator for allocating memory dynamically.
The delete operator is used to release dynamically allocated memory
new Operator
The new operator is used to allocate memory dynamically.
Syntax
pointer = new data_type;
Example
int *p = new int;
This statement creates memory for an integer and stores its address in p.
We can assign a value to the dynamically allocated memory:
*p = 100;
or directly initialize it:
int *p = new int(100);
Now:
cout << *p;
will display:
100
delete Operator
Memory allocated using new should be released when it is no longer required.
The delete operator is used to release dynamically allocated memory.
Example
int *p = new int;
*p = 50;
delete p;
Pointers and Arrays
Pointers and arrays are closely related in C++.
Consider:
int a[5] = {10, 20, 30, 40, 50};
The name of the array, a, generally represents the address of its first element when used in an expression.
Therefore:
a
represents the address of:
a[0]
We can store this address in a pointer.
int *p = a;
Now p points to the first element of the array.
Accessing Array Elements Using a Pointer
int a[5] = {10, 20, 30, 40, 50};
int *p = a;
cout << *p << endl;
cout << *(p + 1) << endl;
cout << *(p + 2) << endl;
Output:
10
20
30
The pointer moves from one array element to the next when we add 1.
Pointer Arithmetic
Pointers can be incremented or decremented.
Suppose:
int a[4] = {10, 20, 30, 40};
int *p = a;
Then:
p++;
makes p point to the next integer in the array.
Similarly:
p–;
moves the pointer to the previous element.
We can also use:
p + 2
p – 1
Pointer arithmetic depends on the data type of the pointer. The compiler automatically moves the pointer by the appropriate number of bytes.
Array of Pointers
An array of pointers is an array in which every element is a pointer.
Syntax
data_type *array_name[size];
Example
int a = 10, b = 20, c = 30;
int *p[3] = {&a, &b, &c};
Here:
- p[0] stores the address of a.
- p[1] stores the address of b.
- p[2] stores the address of c.
To access the values:
cout << *p[0] << endl;
cout << *p[1] << endl;
cout << *p[2] << endl;
Output:
10
20
30
Important
An array of pointers and a pointer to an array are not the same thing
Pointer to an Array
A pointer to an array is a pointer that points to an entire array.
For a one-dimensional array, it can be declared as:
int (*p)[5];
Here, p is a pointer to an array containing 5 integers.
Example
int a[5] = {10, 20, 30, 40, 50};
int (*p)[5] = &a;
Here p stores the address of the entire array a.
The parentheses are important.
int (*p)[5];
is different from:
int *p[5];
The first is a pointer to an array, while the second is an array of pointers.
Function Returning a Pointer
A function can return the address of a variable. Such a function is called a function returning a pointer.
Syntax
data_type* function_name();
Example
int* larger(int *a, int *b)
{
if (*a > *b)
return a;
else
return b;
}
The function returns the address of the larger value.
Example of calling the function:
int x = 25, y = 40;
int *p = larger(&x, &y);
cout << *p;
Output:
40
Pointer to Structure
A pointer can also store the address of a structure variable.
Consider:
struct Student
{
int roll;
float marks;
};
We can create a structure variable:
Student s;
and a pointer to it:
Student *p = &s;
Now p points to the structure variable s
Accessing Structure Members Using a Pointer
There are two ways to access structure members through a pointer.
Using * and dot operator
(*p).roll = 10;
(*p).marks = 85.5;
Using arrow operator ->
p->roll = 10;
p->marks = 85.5;
Both methods produce the same result.
The second method is simpler and is commonly used.
Arrow Operator ->
The arrow operator (->) is used to access the members of a structure or class through a pointer.
Example
struct Student
{
int roll;
float marks;
};
int main()
{
Student s;
Student *p = &s;
p->roll = 5;
p->marks = 90.5;
cout << p->roll << endl;
cout << p->marks << endl;
return 0;
}
Output:
5
90.5
Self-Referential Structure
A self-referential structure is a structure that contains a pointer to another structure of the same type.
This concept is particularly important in linked lists and other dynamic data structures.
Example
struct Node
{
int data;
Node *next;
};
Here Node contains:
Node *next;
which is a pointer to another Node.
This allows several nodes to be connected together.
Quick Revision
- A pointer stores the address of another variable.
- The address-of operator is &.
- The dereference operator is *.
- new is used for dynamic memory allocation.
- delete is used to release dynamically allocated memory.
- An array name generally represents the address of its first element when used in an expression.
- An array of pointers and a pointer to an array are different concepts.
- The -> operator is used to access structure members through a pointer.
- A self-referential structure contains a pointer to the same structure type.
- Self-referential structures are commonly used to implement linked lists.
Frequently Asked Questions
Q1. What is a pointer?
A pointer is a variable that stores the memory address of another variable.
Q2. Which operator is used to obtain the address of a variable?
The address-of operator &.
Q3. Which operator is used to access the value pointed to by a pointer?
The dereference operator *.
Q4. What is the use of new?
new is used to allocate memory dynamically during program execution.
Q5. What is the use of delete?
delete releases memory that was allocated dynamically using new.