Académique Documents
Professionnel Documents
Culture Documents
Question Bank
Pointers
Late Bloomers
Question 1. Describe C++ memory map when a program is executing. Highlight the role and
use of Heap area.
Ans 1. When a program is executed, C++ creates four logically distinct regions of memory:
(i) area to hold the compiled program code
(ii) area to hold global variable
(iii) the stack are to hold the return addresses of function calls, arguments passes to the
functions, local variables for functions, and the current state of the CPU.
the heap area from which the memory is dynamically allocated to the program.
#include<iostream.h>
#include<conio.h>
int *iptr;
char *cptr;
float *fptr;
void main()
{
clrscr();
cout<<"size of integer pointer is :"<<sizeof(iptr)<<endl;
cout<<"size of character pointer is :"<<sizeof(cptr)<<endl;
cout<<"size of float pointer is :"<<sizeof(fptr);
}
output
size of integer pointer is : 4
size of character pointer is : 4
size of float pointer is : 4
Question 3. Given the following definitions:
int ival=2048; int *iptr; double *dptr;
which of the following assignment, if any, are illegal? Explain why.
(a) ival=*iptr (b) *iptr=ival;
(c) *iptr=&ival (d) dptr=iptr;
(e) ival=iptr; (f) iptr=ival;'
(g) iptr=&ival; (h) dptr=*iptr;
Answer 3
a)legal assignment.
(b) legal assignment.
(c)illegal assignment, cannot assign a address of normal variable to pointer variable.
(d) illegal assignment, cannot assign a integer pointer to a double pointer.
(e) illegal assignment, cannot assign pointer variable to a normal variable.
(f) illegal assignment, cannot assign normal variable to a pointer variable.
(g) legal assignment.
illegal assignment, cannot assign a integer pointer to a double pointer
#include <iostream.h>
int main ()
output
float l=7;
int *ptr=&l;
cout<<ptr << endl;
cout<<ptr+1 << endl;
cout<<ptr +2<< endl;
output:
200
204
208
#include <iostream.h>
void main()
{
int a[10];
cout <<”Enter the elements of the array :”;
int sum = 0;
for (int *p=a ; p<a+10; p++)
{
cout<<”Enter the element : “;
cin>>a[i];
sum += *p;
}
cout<< “ Sum of all the elements of the array is :”<< sum
}
#include <iostream.h>
int main()
{
int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
cout << "An integer occupies " << sizeof(int) << " bytes\n";
cout << "\n Number: " << Number;
cout << "\n&number[0]: " << &number[0] << endl;
cout << "\n Number+1: " << Number+1;
cout << "\n&Number:[1] " << &number[1] << endl;
cout << "\n Number+2: " << Number+2;
cout << "\n&Number:[2] " << &number[2] << endl;
return 0;
}
This would produce:
Output :
An integer occupies 4 bytes
Number: 5020
&number[0]: 5020
Number+1: 5024
&Number:[1] 5024
Number+2: 5028
&Number:[2] 5028
#include<iostream.h>
#include<stdio.h>
void main()
{
int count=0;
char *p;
cout<<”Enter the string “;
gets(p);
for ( ; *p!='\0'; *p++)
{ count++; }
cout<<”The length of the string is :”<< count;
getch();
}
Output
Enter the string : kvs
Length of the string is : 3
7. To count the number of vowels in a string
#include<iostream.h>
#include<conio.h>
const int size = 7;
char str[size] = "Computer Science";
int vowels = 0;
for (char *Ptr = str; Ptr < str + size; Ptr++)
{
switch (*Ptr)
{ case 'A':
case 'a':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': vowels++; break;
}
}
cout << str << " has " << vowels << " vowels" << endl;
}
output
Computer Science has 6 vowels
8. Write a program to read the integer in dynamic variable ,calculates and display
its square .
# include<iostream.h>
main()
{
int *p;
p = new int;
cout<<”\n Enter an integer : “;
cin >> *p;
cout<< “\nSquare of the number is : “<<*p * * p;
}
Output
Enter an integer : 5
Square of the number is : 25
Questions for Brilliant learners
1. #include<string.h>
#include<ctype.h>
void main()
{ int i;
char *p=”Student”;
char c;
for (i=1;i<6;i++)
{ if (i%2==0)
++p;
}
c=*p;
cout<<c; }
}
(a) d (b) S (c) u (d) None of these
Ans (c)
4 # 8 # 16 # 20 #
strnset( s1 , 77 , 3);
cout<<s1;
Ans MMMTER
Ans cOMMuTEE
Ans
JackJill
JackJillJohn