CHENNAI PUBLIC SCHOOL, ANNANAGAR
COMPUTER SCIENCE - CLASS XII WORKSHEET TOPIC : POINTERS
1) Predict
the output for the following code:
a) #include <iostream.h>
void
main()
{int
*Queen,Moves[]={11,22,33,44};
Queen
= Moves;
Moves[2]+=22;
cout<<"Queen@"<<endl;
*Queen-=11;
Queen+=2;<<"Next
@"<<*Queen<<endl;
Queen++;
cout<<"Finally@"<<*Queen<<endl;
cout<<"New
Origin @"<<Moves[0]<<endl;}
b) #include
<iostream.h>
#include <string.h>
void FUN(char *str)
{
int L,i,j;
L=strlen(str);
for(i=90;i>=65;i--)
for(j=0;j<L;j++)
if(str[j]==char(i)||str[j]==char(i+32))
cout<<str[j];
cout<<endl;
}
void main()
{
char
*p="Christmas-2016";
FUN(p);
}
c) #include<iostream.h>
void main()
{int A[]={10,15,20,25,30};
int *p=A;
while(*p<30)
{
if
(*p%3!=0)
*p=*p+2;
else
*p=*p+1;
p++;}
for(int j=0;j<=4;++j)
{
cout<<A[j]<<"*";
if
(j%3==0) cout<<endl;
}
cout<<A[4]*3<<endl;
}
d)
#include<iostream.h>
void
main()
{int
Numbers[]={2,4,8,10};
int
*ptr=Numbers;
for(int
c=0;c<3;++c)
{ cout<<*ptr<<"@";
ptr++;}
cout<<endl;
for(c=0;c<4;c++)
{ (*ptr)*=2;
--ptr;}
for(c=0;c<4;c++)
cout<<Numbers[c]<<"#";
cout<<endl;}
e) #include <iostream.h>
#include
<ctype.h>
#include <conio.h>
#include <string.h>
void PointersFun(char
text[],int &count)
{
char *ptr=text;
int length=strlen(text);
for(;count<length-2;count+=2,ptr++)
{
*(ptr+count)=toupper(*(ptr+count));
}}
void main()
{
clrscr();
int position=0;
char
data[]="ChangeString";
PointersFun(data,position);
cout<<data<<"@"<<position;
cout.write(data+3,4);
}
f) #include
<iostream.h>
void main()
{char *Text=²AJANTA²;
int *P, Num[]={1,5,7,9};
P=Num;
cout<<*P<<Text<<endl;
Text++;
P++;
cout<<*P<<Text<<endl;
}
g)
#include <iostream.h>
#include <string.h>
#include <conio.h>
char * MyFunction(char
RECEIVE_TEXT[])
{
RECEIVE_TEXT+=5;
cout<<*RECEIVE_TEXT<<"
"<<strlen(RECEIVE_TEXT)<<endl;
return RECEIVE_TEXT;
}
void main()
{
char
*SAMPLETEXT="IMAGINATION";
cout<<MyFunction(SAMPLETEXT)<<endl;
SAMPLETEXT++;
*SAMPLETEXT=*SAMPLETEXT+3;
cout<<SAMPLETEXT;}
h) #include
<iostream.h>
void main()
{
int a=32,*ptr=&a;
char ch=’A’.&cho=ch;
cho+=a;*ptr+=ch;
cout<<a<<”
“<<ch<<endl;
}
i) Distinguish between
int *ptr=new int(5);
int *ptr=new int[5];
j) What are the operators associated
with pointer variable? Explain with example.
k) Illustrate the use of “self-referential structures” with the
help of an example.
l) Differentiate between static and dynamic
allocation of memory.
No comments:
Post a Comment