Professional Documents
Culture Documents
003 Chapter7-Pointers
003 Chapter7-Pointers
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
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)
{
} [Y21
Q. 2 Write the definition of a member function } ;
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
-
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()
{
{
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++; {
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)
{ }
cout«a«"@"«b«"@"«c«"@"«endl;
x*.y; out(a,b,c);
y++;
z/=(x+y); c o ut «a «" $" « b« "$ "« c« " $" «e nd l;
}
}
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;
EduSuccess Smart Learning Centers I www.edusuccess.in I Contact # +91- 85 0606 1616 / 5252