Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

CHAPTER

POINTERS

Syllabus
• Introduction to Pointer, Declaration and Initialization of Pointer; Dynamic memory allocation!
deallocation operators : new, delete; Pointers and Arrays : Array of Pointers, Pointer to an array
(1 dimensional array), Function returning a pointer, Reference variables and use of alias; Function
call by reference. Pointer to structure : De-referencelReference operator: *, ->; self referential
structure;

Quick
Review
1. Introduction
A pointer is a variable which holds a memory address. Any variable declared in a program has two components:
(i) Address of the variable
(ii) Value stored in the variable
For example: int p = 31;
The above declaration tells the C+ + compiler for
(i) Reservation of space in memory for storing the value.
(ii) Associating the name p with this memory location.
(iii) Storing the value 31 at this location.
Location name —>
Value at location
Location —> 1001
2. Static Memory Allocation
When the amount of memory to be allocated is known in advance and memory is allocated during compilation,
it is referred to as static memory allocation. For example:
int a = 10;
3. Dynamic Memory Allocation
When the amount of memory to be allocated is not known in advance and memory is allocated during execution,
it is referred to as dynamic memory allocation. There are two operators— new and delete in C+ +, for dynamic
memory allocation. The operator new allocates memory dynamically whereas the operator delete is used for
deallocation, when the memory is no longer in use.
4. Declaration and Initialization of Pointers
A pointer variable name is preceded by after asterisk (*) sign. The data type of the pointer sho uld be the same as
the data type of the variable to which it will point.
Syntax: type *var_name;
Example;
int *iptr; //declaration of an integer pointer
int p = 21;
iptr = &p; //iptr stores the address of integer
variable p

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class — 12

C + + has two unary operators for referencing to the components of any variable & (address operator) returns the
address of the variable while * (direction operator) returns the value stored at any address in the memory.
Address of p 1050 int p = 21
p 21 int * iptr;
iptr —> I1050 iptr = &p;
j 21 j =13;
new operator
Syntax: pointer variable = new data type;
Example :
char *cptr;
cptr = new char;
char *captr;
captr = new char[10];
delete operator
Syntax: delete pointer variable;
Example:
delete cptr;
delete [] captr;
C + + has the concept of constant pointer and pointer to a constant. For example:
char * const cptrl = "computer science"; //constant pointer
Here the address of cptrl cannot be modified.
int const *iptr = &x; //pointer to a constant
const char * const cptr2="KV"; //pointer to a constant
Here the constant value, to which the pointer pointing to, can not be modified.
A NULL pointer is a pointer which indicates that it is not pointing to any valid memory address. For example:
int * iptr = NULL;
5. Pointer Arithmetic
Only addition and subtraction may be performed on pointers. All the pointers increases and decreases by the
length of the data type they point to. Adding 1 to a pointer adds the size of pointer A's base type. Let iptr be an
integer pointer, currently pointing to memory address 2002. If the int size is 2 bytes, then after the execution of:
iptr+ + ;
iptr will be pointing to 2004.
6. Arrays and Pointers
An array name is equivalent to a pointer pointing to the first element of array. The address of the first byte is
called Base Address. In C + +, we may have an array of pointers also.
7. Functions and Pointers
A function may return a reference or a pointer variable. Pointer to a function can be passed to function, and can
return from functions.
8. Structure and Pointers
C+ + allows pointers to structures like other data types and these pointers to structures are kn own as structure
pointers.
Syntax: struct_name struct_pointer;
The member of the structure are accessed by using " (arrow operator).
Syntax: struct-pointer " struct_member;
9. Objects and Pointers
C + + allows us to have pointers to objects known as object pointers.
Syntax: Class_name object_pointer;
10. t h i s P o i n te r s
While defining a class, the space is allocated for member functions only once and separate space is allocated for
each object. There exists a serious problem i.e., which object's data member is to be manipulated by any member
function.

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
POINTERS LEARNING CENTERS FOR CLASSES VI -XII

11. Pointer to Pointers


We can define pointer pointing to another pointer, that points to the target value.
Syntax: int **ptr_to_ptr;
In the case of pointer to a pointer, the first pointer contains the address of the second pointer, which points to the
objects that contains the desired value.

Know the Terms


 address : A memory location.
 allocator : Object used by standard library containers to allocate and deallocate memory.
 free() : C standard deallocation function.
 pointer : An object holding an address or 0.
 smart pointer : User-defined type providing operators like a function, such as * and + +, with a semantics
similar to pointers. The smart pointer is sometimes known as handle.
 this : Pointer to the object for which a non-static member function is called.
 void* : Pointer to void; that is, a pointer to an object of unknown type; also called pointer to raw memory. A
void* cannot be used or assigned without a cast.

Very Short Answer Type Questions [1 mark each]

Q. 1 Write the definition of a function FixPay (float Pay[], int N) in C+ +, which should modify each element of the
array Pay having N elements, as per the following rules : [Delhi, 2016]
Modification
Existing Salary Value Required
in Value
If less than 1,00,000 Add 25% intheexisting value
If >=1,00,000 and <20,000 Add 20% in the existing value
If >=2,00,000 Add 15% in the existing value
Ans. Void FixPay (float Pay[],int N)
{

for (int 1.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;

} [Y21
Q. 2 Write the definition of a member function } ;

INSERT() for a class QUEUE in C+ +, to remove Ans. Void QUEUE : INSERT ( )


a product from a dynamically allocated Queue of
items considering the following code is already ITEM * newitem = new ITEM;
written as a part of the program. [Delhi, 20161 Cout«llenter item number"
Struct ITEM cin»newitem — INO;
Cout«'Enter item name";
{

int INO; char INANE [20] ; gets (newitem INAME);


newitem -4 Link = NULL;
ITEM*Link;
}; if (R==NULL)
class QUEUE
{
R=F=newitem;
else
ITEM *R, *F; {

Public :
QUEUE() {R=NULL; F=NULL;) R —>Link=newitem; [Y2]
R = newitem;
void INSERT(); }

void DELETE(); }

-QUEUE();

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
LEARNING CENTERS FOR CLASSES VI -XII EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class 12
-

Short Answer Type Questions-I [2 marks each]

Q. 1. Write the output from the following C+ + Q. 3. Obtain the output from the following C+ +
program code : [CBSE SQP 2015] program as expected to appear on the screen
#include<iostream.h> after its execution.
#include<ctype.h> Important Note :
void strcon(char s[])  All the desired header files are already
{
included in the code, which are
for(int i=0,1=0;s[i]!='\01;i++,1++); required to run the code.
for(int j=0; j<1; j++) void main()
{
{

if (isupper(s[j])) char *String="SARGAM";


s[j]=tolower(s[j])+2; int *Ptr, a[]={1,5,7,9};
else if ( islower(s[j])) ptr=a;
s[j]=toupper(s[j])-2; cout«*ptr«String«endl;
else String++;
s[j]='@'; ptr+=3;
} cout«*ptr«String«endl;
}
} [Delhi 2014]
void main() Ans. 1SARGAM [1]
9ARGAM [1]
char *c="Romeo Joliet"; Q.4. Give the output of the following program
strcon(c); segment: (Assuming all desired header file(s) are
cout«"Text= "«c«endl; already included)
void main()
c=c+3;
cout«"New Text= "«c«endl;
5JANTAPI float *Ptr, Points[] = {20,
c=c+5-2;
50, 30, 40, 10};
cout«"last Text= "«c ;
Ptr = points;
}

cout«*Ptr«endl;
Ans. Text = tMKCM@__1MJGCR [1]
Ptr+=2;
New Text = KCM@1MJGCR [1/2]
Points[2]+=2.5;
1
Last Text = 1MJGCR [ /2] cout«*Ptr«endl;
Q. 2. Obtai n t he output of t he following C+ + Ptr++;
program as expected to appear on the screen
(*Ptr)+=2.5;
after its execution.
cout«Points[3]«endl;
Important Note : } [CBSE Comptt. 2013]
 All the desired header files are already Ans. 20.00
included in the code, which are required to
32.5
run the code.
42.50 [2]
void main()
{
Q. 5. Find the output of the following code :
char *Text="AJANTA"; Important Note :
int *P, Num[]={1,5,7,9} All the header files are already included in the
code, which are required to run the code.
P=Num;
[Delhi, 2013]
cout «*p« Text «endl;
void main()
Text++; {

P++; char *String="SHAKTI";


cout«*P«Text«endl; int*Point,Value[]={10,15,70,19}
[0.D. 2014, 2013] Point=Value;
Ans. 1AJANTA PI cout«*Point«String«endl;
String++;
Point++;
EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
POINTERS LEARNING CENTERS FOR CLASSES VI -XII

cout«*Point«String«endl; else
{

Ams.10SHAKTI if (*s%,2==0)
15HAKTI *s=*s-10;
(1 mark for each correct line of output) else
Q. 6. Write the output of the following C+ + program * s = * s - 1 1 ;
code :
Note : Assume all required header files are cout«*s«"
already being included in the program. s++;
void change(int*s)
{ }

for(int i=0;i<4;i++) void main()


{ {
if (*s<40) int score[]={25,60,35,53};
{ change(score);
if (*s%2=.0) [CBSE SQP 2016]
*s=*s+10; Ans. 36 50 46 42
else
(1/4 mark
*s=*s+11; for each correct value of output)
}
[CBSE Marking Scheme, 2016]

Short Answer Type Question-II


Q. 1. Find the output of the following program :
[CBSE Comptt., 2014]
#include<iostream.h> [3 marks]
void in (int x, int y, int &z) void main()
{ {

x+=y; int a=20, b=30, c=10;


Y--; out(a,c,b);
z*=(x-y);
}
cout «a«"#"«b«" #"«c«"#"«e ndl;
void out(int z,int y, int &x) in(b,c,a);
{

cout«a«"@"«b«"@"«c«"@"«endl;
x*.y; out(a,b,c);
y++;
z/=(x+y); c o ut «a «" $" « b« "$ "« c« " $" «e nd l;
}
}

Ans. 20#300#10# [1]


620@300@10@ [1]
620$300$3000$ [1]

Long Answer Type Questions [4 marks each]


Q. 1. Find the output of the following code: cout«"Rest To"«*Track[0]«endl;
#include<iostream.h> [Delhi, 2011]
void main ( ) Ans. Striker10
Next@55
int *Striker; Last@55
int Track [] ={10,25,30,55}; Rest To 0
Striker=Track; Track [1] +.30; (1 mark for each correct line of output)
cout«"Striker"«*Striker«endl; Q. 2. Find the output of the following code :
*Striker=-10; #include<iostream.h>
Striker++; void main()
cout«"Next@"«*Striker«endl; { int *Queen;
Striker+=2; Moves[]={11,22,33,44};

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252
cout«"Last@"«*Striker«endl;
LEARNING CENTERS FOR CLASSES VI -XII EduSuccess : Delhi's #1 Tuition Center for Computer Science, Class — 12

Queen=Moves; cout«"NewOrigin@"«*Moves[0]«endl;
Moves[2]+=22; } [0.D, 2011]
cout«"Queen@"«*Queen«endl; Ans. Queen@11
*Queen- =11; Now@55
Queen+=2 ; Finally@44
cout«"Now@"«*Queen«endl; NewOrigin@O
Queen++ ; (1 mark for each correct line of output)
cout«"Finally@"«*Queen‹<endl;

Know the Links


Download Chapterwise notes, study materials, Ten Year Solved Papers, Important Questions
of Physics, Chemistry, Maths, Biology, Computer Science from www.edusuccess.in

EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252

You might also like