CHAPTER 11 Completed
CHAPTER 11 Completed
Ch -11 POINTERS
1
NANDI PU COLLEGE
11.is the structure that include an element that points to another structure
of same type
(a) Memory heap (b) Self-referential structure
(c) Structure (d) Memory pool
2
NANDI PU COLLEGE
1 2 3 4 5 6 7 8 9 10
a a a c b a b b a c
11 12 13 14 15 16 17 18 19 20
b a a b a d a c a b
21 22 23 24 25
a d a b b
Key Answers:-
Q.2 How dynamic memory allocation is different from static memory allocation?
A:
Static Memory Allocation Dynamic Memory Allocation
The memory allocation occurs at The memory allocation occurs at
compilation time. execution time or run time.
Variables remain allocated until the Variables remain allocated until the
program is active. program unit is active.
Implemented using stacks and heaps. Implemented using data segments.
Wastage of memory space occurs. No wastage of memory space. Thus, it
Thus it is not an efficient technique. is an efficient technique.
The amount of memory to be allocated The amount of memory to be allocated
is pre-known. is not known.
Q.3 Illustrate the use of “self referential structure” with the help of an example?
A: The self referential structures are structures that include an element that is a pointer to another
structure of the same type.
Eg:- struct student
3
NANDI PU COLLEGE
{
int regno;
char name[25];
float fees;
struct student *next;
};
Also assume that, the list consists of three nodes n1,n2 and n3.
struct student n1,n2,n3;
In the above example, student is a “self referential structure”.
Q.6 Show the general form of new and delete operator in C++?
A: The general form of new operator in C++ is:-
datatype *pointe-rvariable;
pointer_variable = new datatype;
4
NANDI PU COLLEGE
#include<iostream.h>
#include<conio.h>
void main( )
{
int i ,n , *ptr , *a[10];
cout<<” Enter the size of an array “<<endl;
cin>>n;
cout<<” enter array elements “<<endl;
for(i=0; i<n; i++)
cin>>a[i];
cout<<” The array elements are “<<endl;
for(i=0; i<n; i++)
cout<<*ptr[i]<<setw(4)<<endl;
}
Q.8 What is the relationship between array and pointers? Give example.
A: An array stores the similar type of elements in contiguous memory locations for efficient access.
To access all such values using pointers, an initial address of the array must be assigned to the pointer.
Later such elements can be accessed by incrementing the counter.
Q.9 What is the relationship between strings and pointers? Give example.
A: A pointer variable must be declared and starting address of the string must be assigned to a
pointer. It points to first character of an array. Later by incrementing the counter rest of the
characters can be accessed.
C O M P U T E R S C I E N C E \0
s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9] s[10] s[11] s[12] s[13] s[14] s[15] s[16]
where,
struct_name is the name of defined structure and
struct_pointer is the pointer to a structure.
where,
dateptr is a pointer to a structure date.
Q.11 What is the relationship between object and pointer? Give example.
A: - A pointer pointing to an objects are referred as “object pointers “.
Just like other pointers, object pointers are declared by placing “ * “ in front of
object pointer name.
Syntax:- class_name *object_pointer;
where,
class_name is the name of defined class and
object_pointer is the pointer to an object of class type.
Eg:- #include<iostream.h>
6
NANDI PU COLLEGE
#include<conio.h>
class student
{
private:
int regno;
char name[20];
float fees;
public:
void input( );
void output( );
};
void student ::input ( )
{
cout<<” Enter student register number, name and fees “<<endl;
cin>>regno>> name>>fees;
}
void student :: output( )
{
cout<< “ Student register number = “<<regno<<endl;
cout<<”Student name =”<<name<<endl;
cout<<” Student fees = “<<fees<<endl;
}
void main( )
{
student s, *sp;
sp = &s;
sp ->input( );
sp ->output( );
getch( );
}