Pages

Wednesday, December 21, 2011

Questions on Pointers

 
 
 
POINTERS

Predict the output
a)
#include<iostream.h>
#include<conio.h>
int a=3;
void demo(int &x,int y, int *z)
{
clrscr();
a + =x;


y *= a;
*z=a+y;
cout << a<< endl<< x<< endl<< y<< endl<< *z<< endl;
}
main()
{
int a=2,b=5;
demo (::a , a, &b);
cout<<::a<<endl<<a<<endl<<b;
}
b)
#include<conio.h>
#include<iostream.h>
main()
{
clrscr();

int a=32, *x=&a;
char ch=65, &cho=ch;
cho += a;

*x += ch;
cout<<a<<" "<<ch;
}
c)
#include<iostream.h>
void change(int *);
void main()
{
int a[5] = {4,5,6,7,8};
change(a);
for (int i =4; i>=0; i--)
cout<<a[i];
cout<<”\n”;
}
void change(int *b)
{
for(inti=0; i <=4 ;i++)
{
*b = *b +1;
b++;
}
}
d) #include<iostream.h>
void main()
{
int arr[]={10,11,12,13,14};
int i,*p;
for (p=arr, i=0 ; p+i <= arr+4; p++, i++)
cout << *(p+i);
cout << ”\n”;
}
d)
void main()
{
char a[] = ”able was I ere I saw elba”;
char *t, *s, *b;
s = a;
b = a + strlen(a) - 1;
t = b;
while (s != t)
{
cout << *s;
s++;
cout << *t;
t--;
}
}
e) #include<iostream.h>
void main()
{
int arr[]={10,12,14,16,18};
int *p;
for (p=&arr[0]; p<=&arr[4]; p++)
cout<<*p;
cout<<”\n”;
}
f) #include <iostream.h>
int main()
{
int a, *b, **c;
a=12;
b=&a;
c=&b;
cout<<a<<a+*b;
cout<<**c;
}
g) #include<iostream.h>
#include<conio.h>
void junk(int, int *);
void main()
{
int i=6, j=-4;
junk (i, &j );
cout<<”i = “ <<i<<”j=”<<j;
}
void junk(int a, int *b)
{
a = a * a;
*b = *b * *b;
}
h) #include<iostream.h>
#include<conio.h>
void 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”<<”\n”;
}
return 0;
}
void fun(int p, int *q)
{
p=*(q) +=2;
}
Define an array. Define a pointer. What is the relationship between an array and a pointer. Given below a function to traverse a character array using for loop. Use pointer in place of an index X and substitute for-loop with while loop so that the output of the function stringlength() remains the same.
Int strlength(char s[])
{
int count=0;
for(int x=0; s[x]; x++)
count++;
return(count);
}
What is the mistake in the following statement
Char *ptr, s[10];
Ptr=s[6];
Find out the errors if any in the following program
#include<iostream.h>
#include<conio.h>
main()
{
int check(int, int);
int *c;
c=check(10, 20);
cout<<*c;
return 0;
}
int check(int i, int j)
{
int *p, *q;
p=&i;
q=&j;
if(i>=50)
return p;
else
return q;
}
Differentiate between static and dynamic allocation of memory
How does the functioning of a function different when
i) an object is passed by value
ii) an object is passed by reference
What is ‘this’ pointer? What is its significance