You are on page 1of 6

Month August 2008

Page 1 of 6

POINTER ASSIGNMENT
Computer Science
1.

a) Predict and Explain the output of following Program:


#include<iostream.h>
#include<conio.h>
void junk( int , int *);
int main()
{
clrscr( );
int I =6, j = -4;
junk( i , & j);
cout<<i= << i << j = << j << \n;
return 0;
}
void junk( int a, int *b)
{
a = a*a;
*b= *b * *b;
}
b) #include<iostream.h>
#include<conio.h>
int main( )
{
clrscr ( );
float x = 5.99;
float * y, * z;
y = &x ;
z = y;
cout<<x<< , << ( &x) << , << *y << ,<< *z <<\n;
return 0;
}
c) Identify and explain the error(s) in the following code snippet:
#include<iostream.h>
#include<conio.h>
int main( )
{
clrscr ( );
float a [] = { 11.02, 12.13, 19.11, 17.21 };
float * j , *k;
j =a;
k =a +4;
j= j * 2;
k= k /2;

4 /3
marks
questions

Month August 2008


cout<< * j = << *j << *k = << k <<\n;
}
d) Predict and explain the output of the following program:
#include<iostream.h>
#include<conio.h>
int main( )
{
clrscr ( );
void func( int , int * );
int a[5] = { 2, 4, 6, 8, 10 };
int i , b= 5;
for ( i =0 , i < 5 ; i ++)
{
func( a[i], &b);
cout <<a[i] << \t<< b<<\n;
}
returon 0;
}
void func ( int {, int *q)
{
P = *(q) + =2;
}
e) The code and the output of a program has been given below.
Explain the reasons for this output.

#include<iostream.h>
#include<conio.h>
int main( )
{
clrscr ( );
char str []= Sansum ;
char *s;
s = & str[5] 5;
while ( *s)
cout<<*s++;
cout<< \n Thanks\n;
return 0;
}
Identify the syntax error(s) , if any , in the following program. Also
give reason for errors.
Void main()
{

Page 2 of 6

Month August 2008


const int i = 10;
const int * const ptr = &i ;
*ptr ++;
int j = 15;
ptr = &j;
}
3

b)
a) What is the output of the following program :
Char * Name = IntRneT;
for ( int x =0; x < strlen(Name); x++)
if ( islower(Name[x]))
Name[x] = toupper( Name[x]);
else
Name[x] = Name[x-1];
Puts(name );
b) Give the output of the following porgarm:
Void main( )
{
char *p = Difficult;
char c;
c= ++*p ++;
printf(%c,c);
}
c) Give the output of the following program segment.
Char * Name = a ProFiLe;
for( int x = 0; x < strlen( Name) ; x ++)
if ( islower (Name[x]))
Name[x] = toupper( Name[x]);
else
if(isupper(Name[x]))
if(x%2 != 0)
Name[x] = tolower(Name [x-1]);
else
Name[x]-- ;
cout<<Name<<endl;
d) Give the output of the following program
Void main ( )
{
int array[ ] = { 2, 3, 4, 5 };
int * arptr = array ;
int value = * arptr ; cout<<value <<\n ;
value = * arptr++ ; cout<<value <<\n ;
value = * arptr ; cout<<value <<\n ;

Page 3 of 6

Month August 2008


value = * ++arptr ; cout<<value <<\n ;
}
e) Find the output of the following programs:
#include< iostream.h>
void main ( )
{
int array[] = { 4, 6, 10, 12 };
int *pointer = Array;
for ( int i =1; i<=3; i++)
{
cout<<*pointer<<#;
pointer++;
}
cout<<endl;
for ( i=1; i<=4; i++)
{
(*pointer)*= 3;
--pointer;
}

4.

for(i=1; i<5; i++)


cout<<Array[i-1] <<@;
cout<<endl;
}
identify the syntax error(s) , if any , in the following program.
a) void main( )
{
const int i = 20;
const int * const ptr = &i ;
( *ptr) ++;
int j = 15;
ptr = &j;
}
b) #include <iostream.h>
main ( )
{
int x[5], *y, z[5];
for( i=0 ; i<5; i++
{
x[i] = i ;
z[i] = i+3;
y = z;
x = y;
}
}

Page 4 of 6

Month August 2008

a. Difference between static and dynamic allocation in momory.


b. What do you understand by memory leaks? What are the possible
reasons for it? How can memory leaks avoided?
c. Distingusih between
int *ptr = new int(5);
int * ptr = new int [5];
d. How does the function differ when
i. an object is passed by value?
ii. and object is passed by reference ?
e) What is this pointer ? What is its significance?
f) What is the relation between the array and pointer.
g) How is *p different from **p?
h) How is &p different from *p?
i) Find the error in following code segment:

Some Important Example of Pointers:


POINTERS TO OBJECT
#include<iostream.h>
class item
{
int code; float price;
public :
void getdata( int a, float b)
{ code = a ; price = b; }
void show( void )
{ cout<< Code = <<code<<\n;
cout<<price =<<price<<\n; }
};
const int size =2;
main( )
item *p = new item [size] ;
item *d = p;
int x, i ;
float y;
for ( i =0 ; i < size ; i++)
{ cout<< Input Code and Price for Item << i+1;
cin>>x>>y;
p-> getdata (x, y);
p++;
}

Page 5 of 6

1/2
marks
quesions

Month August 2008

Page 6 of 6

for ( i=0 ; i<size; i++)


{
cout<< Item : << i+1<<\n;
d->show( );
d++;
}
}

------------------------------------