Chapter 7: Pointers Class 12 Computer Science NCERT Solutions
Pointers are one of the most powerful and essential features of C++ programming. In Chapter 7 – Pointers, students are introduced to the concept of memory addresses and how they can be accessed and manipulated directly using pointers. This chapter explains how pointers enhance memory management, enable dynamic memory allocation, and play a key role in object-oriented programming, especially in relation to arrays, functions, and objects.
What You Will Learn in Chapter 7
This chapter helps students build a clear understanding of what pointers are, how they are declared and used, and why they are important in C++. Through examples and practical applications, students will learn how to perform operations involving pointer variables and how to use pointers for indirect data access and efficient programming.
Key Topics Covered
Introduction to Pointers
Definition of a pointer: A pointer is a variable that stores the memory address of another variable.
Why pointers are important: Useful for dynamic memory allocation, array and function handling, and efficient program execution.
Syntax:
int *ptr;
(declares a pointer to an integer)
Address-of (&) and Dereference (*) Operators
& Operator: Returns the memory address of a variable.
Example:ptr = &x;
* Operator: Used to access the value at a memory address (dereferencing).
Example:cout << *ptr;
displays the value ofx
ifptr = &x;
Declaring and Using Pointers
Declaring pointer variables:
int *p; float *fptr; char *chptr;
Initializing pointers: Assigning the address of a variable using
&
.Dereferencing pointers to access or modify the data stored at the pointed location.
Pointer Expressions and Pointer Arithmetic
Incrementing and decrementing pointers:
ptr++
,ptr--
(moves pointer to next/previous memory block depending on data type size)Adding/subtracting integers to/from pointers.
Comparing pointers: Useful when traversing arrays using pointers.
Pointers and Arrays
Pointers as an alternative to array indexing.
Example:*(arr + i)
is equivalent toarr[i]
Accessing elements using pointers.
Relationship between arrays and pointers (array name as base address).
Using pointer notation in loop structures.
Pointers and Functions
Passing variables by reference using pointers.
Enables a function to modify the actual arguments passed to it.Function with pointer parameters:
void swap(int *a, int *b);
Pointers for dynamic function calls and returning addresses.
Dynamic Memory Allocation
Using
new
anddelete
operators:int *ptr = new int;
(allocates memory dynamically)delete ptr;
(frees the memory)
Creating and deleting dynamic arrays using pointers:
int *arr = new int[10];
delete[] arr;
Pointers to Structures and Objects
Creating structure pointers and accessing members using
->
operator. Example:student *ptr = &s; cout << ptr->name;
Working with pointers to objects and calling member functions using
->
.
Null Pointer and Dangling Pointer
NULL pointer: Pointer that points to nothing. Initialized using
nullptr
orNULL
.Dangling pointer: A pointer pointing to a memory location that has been deleted or released.
Common Errors in Pointer Usage
Uninitialized pointers leading to garbage values.
Memory leaks due to improper use of
new
anddelete
.Incorrect dereferencing causing runtime errors.
Download Chapter 7 Solutions PDF – Pointers
Our downloadable PDF includes:
Complete NCERT textbook solutions with explanation.
Code snippets for pointer arithmetic, functions, and arrays.
Output-based questions with traceable memory diagrams.
Step-by-step logic for dynamic memory allocation programs.
Summary tables for pointer syntax and operations.
Highlights of Our NCERT Solutions
Clear definitions and real-life analogies to explain abstract concepts.
Memory diagrams to visualize address manipulation and pointer behavior.
Sample programs for swapping values, array traversal using pointers, and memory allocation.
Differentiation between pointer to a variable, pointer to array, and pointer to object.
Common error cases highlighted with debugging strategies.
Recommended Preparation Tips
Practice pointer basics: declaration, initialization, dereferencing.
Create small programs to test pointer arithmetic with arrays.
Use dry runs and diagrams to trace pointer movement.
Implement swap functions using pointers to understand reference passing.
Practice dynamic memory allocation and deallocation for both single variables and arrays.
Additional Study Resources
Pointer Flashcards: Operators, syntax, and error messages.
Practice Sheets: Fill-in-the-blank, memory tracing exercises.
Debugging Drills: Identify and fix common pointer-related bugs.
Sample Programs: Pointer with arrays, functions, objects, and structures.
CBSE PYQs: Important exam-style questions involving pointers.
Mastering Chapter 7 – Pointers
Pointers are a fundamental part of C++ programming. A deep understanding of pointers not only helps in building more efficient code but also lays the foundation for understanding linked lists, trees, and other dynamic data structures. They also play a crucial role in object-oriented programming, especially when dealing with constructors, destructors, and inheritance.
Once students are comfortable with pointer syntax and memory handling, they are ready to tackle more advanced data structure implementation in C++.
Class 12 Computer Science (C++) – Chapter 7 Pointers
Very Short Answer Type Questions [1 mark each]
Question 1: Write the definition of a function FixPay(float Pay[ ], int N) in C++, which modifies each element of the array Pay having N elements as per the following rules:
Existing Salary Value Less than ₹1,00,000 → Add 25% From ₹1,00,000 to less than ₹2,00,000 → Add 20% ₹2,00,000 and above → Add 15%void FixPay(float Pay[], int N) {
for (int i = 0; i < N; i++) {
if (Pay[i] < 100000)
Pay[i] += Pay[i] * 0.25;
else if (Pay[i] < 200000)
Pay[i] += Pay[i] * 0.20;
else
Pay[i] += Pay[i] * 0.15;
}
}
Question 2: Write the definition of a member function INSERT() for class QUEUE in C++ to insert an item into a dynamically allocated queue.
struct ITEM {
int INO;
char INAME[20];
ITEM* Link;
};
class QUEUE {
ITEM *R, *F;
public:
QUEUE() { R = NULL; F = NULL; }
void INSERT();
void DELETE();
~QUEUE();
};
void QUEUE::INSERT() {
ITEM* newitem = new ITEM;
cout << "Enter item number: ";
cin >> newitem->INO;
cin.ignore();
cout << "Enter item name: ";
cin.getline(newitem->INAME, 20);
newitem->Link = NULL;
if (R == NULL) {
R = F = newitem;
} else {
R->Link = newitem;
R = newitem;
}
}
Question 3: Write the output of the following C++ code:
#include<iostream.h>
#include<ctype.h>
void strcon(char s[]) {
for(int i = 0, l = 0; s[i] != '\0'; i++, l++);
for(int j = 0; j < l; j++) {
if(isupper(s[j]))
s[j] = tolower(s[j]) + 2;
else if(islower(s[j]))
s[j] = toupper(s[j]) - 2;
else
s[j] = '@';
}
}
void main() {
char *c = "Romeo Joliet";
strcon(c);
cout << "Text = " << c << endl;
c = c + 3;
cout << "New Text = " << c << endl;
c = c + 3;
cout << "Last Text = " << c;
}
Output:
Text = tMKCM@lMJGCR
New Text = MKCM@lMJGCR
Last Text = lMJGCR
Question 4: Output of the following C++ code:
void main() {
char *Text = "AJANTA";
int *P, Num[] = {1, 5, 7, 9};
P = Num;
cout << *P << Text << endl;
Text++;
P++;
cout << *P << Text << endl;
}
Output:
1AJANTA
5JANTA
Question 5: Output of the following program:
void main() {
char *String = "SARGAM";
int *Ptr, a[] = {1, 5, 7, 9};
Ptr = a;
cout << *Ptr << String << endl;
String++;
Ptr += 3;
cout << *Ptr << String << endl;
}
Output:
1SARGAM
9ARGAM
Question 6: Output of the following code segment:
void main() {
float *Ptr, Points[] = {20, 50, 30, 40, 10};
Ptr = Points;
cout << *Ptr << endl;
Ptr += 2;
Points[2] += 2.5;
cout << *Ptr << endl;
Ptr++;
(*Ptr) += 2.5;
cout << Points[3] << endl;
}
Output:
20
32.5
42.5
Question 7: Output of the following C++ program:
void main() {
char *String = "SHAKTI";
int *Point, Value[] = {10, 15, 70, 19};
Point = Value;
cout << *Point << String << endl;
String++;
Point++;
cout << *Point << String << endl;
}
Output:
10SHAKTI
15HAKTI
Question 6:
Output of the following C++ code:
void change(int *s) {
for(int i = 0; i < 4; i++) {
if(*s < 40) {
if(*s % 2 == 0)
*s = *s + 10;
else
*s = *s + 11;
} else {
if(*s % 2 == 0)
*s = *s - 10;
else
*s = *s - 11;
}
cout << *s << " ";
s++;
}
}
void main() {
int score[] = {25, 60, 35, 53};
change(score);
}
Output: 36 50 46 42
Short Answer Type Question-II
Question 1:
Find the output of the following program:
#include<iostream.h>
void in(int x, int y, int &z) {
x += y;
y--;
z *= (x - y);
}
void out(int z, int y, int &x) {
x *= y;
y++;
z /= (x + y);
}
void main() {
int a = 20, b = 30, c = 10;
out(a, c, b);
cout << a << "#" << b << "#" << c << "#" << endl;
in(b, c, a);
cout << a << "®" << b << "@" << c << "@" << endl;
out(a, b, c);
cout << a << "$" << b << "$" << c << "$" << endl;
}
Output:
20#300#10#
6020®300@10@
6020$300$3000$
Step-by-Step Explanation:
Step | Function | a | b | c | Explanation |
---|---|---|---|---|---|
1 | out(a, c, b) | 20 | 300 | 10 | b is updated to 30×10 = 300 (by reference) |
2 | in(b, c, a) | 6020 | 300 | 10 | a = a × (310−9) = 20 × 301 = 6020 |
3 | out(a, b, c) | 6020 | 300 | 3000 | c |
Long Answer Type Questions
Question 1:
Find the output of the following code:
#include<iostream.h>
void main() {
int *Striker;
int Track[] = {10, 25, 30, 55};
Striker = Track;
Track[1] += 30;
cout << "Striker" << *Striker << endl;
*Striker = -10;
Striker++;
cout << "Next@" << *Striker << endl;
Striker += 2;
cout << "Last@" << *Striker << endl;
cout << "Rest To" << *Track[0] << endl;
}
Output:
Striker10
Next@55
Last@55
Rest To0
Question 2:
Find the output of the following code:
#include<iostream.h>
void main() {
int *Queen;
int Moves[] = {11, 22, 33, 44};
Queen = Moves;
Moves[2] += 22;
cout << "Queen@" << *Queen << endl;
*Queen -= 11;
Queen += 2;
cout << "Now@" << *Queen << endl;
Queen++;
cout << "Finally@" << *Queen << endl;
cout << "NewOrigin@" << *Moves[0] << endl;
}
Output:
Queen@11
Now@55
Finally@44
NewOrigin@0