Practice Questions of Pointers

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Practice questions of Pointers

A. Find the output of the following program codes, assuming all necessary header file(s) are included:
1. void main( )
{ int X[ ]={20, 25, 40, 85, 100};
int *p=X;
for(int i=1;i<5;i++)
cout<<*p++<<”#”;
while(*p>20)
{ if(*p%5!=0)
*p = *p + 1;
else
*p = *p + 2;
p - -;
. }
cout<<endl;
for(i=4;i>=1;i--)
cout<<X*i+<<”*”;
}

2. void main( )
{ int track[ ] = {10,20,30,40}, *strick;
strick = track;
track[1] +=20;
cout<<*strick<<endl;
*strick - = 10;
strick++;
cout<< *strick<<endl;
strick=track;
for(int i=0;i<4;i++)
cout<<*strick++;
}

3. void secret(char message[],int n)


{
for (int c=0;message*c+!=’\0’;c++)
if(c%2==0) message[c]=message[c]+n;
else if (isupper(message[c])) message[c]=tolower(message[c]);
else message[c]=message[c]-n;
}

void main()
,char *sms=”JAgoIndiAJAgo”;
secret(sms,2);
cout<<sms<<endl;
}
4. void main( )
{
const int x;
int *p=&x;
p++;
cout<<++*p;
}
5. void main()
{
char a*+= “PreBoARd ExAm-2018-19”;
char *p=a;
for( ; *p!= ‘\0’;p++)
{
if(isupper(*p))
++*p;
else if(isdigit(*p))
*p=*p+1;
else if(islower(*p))
*p=toupper(*p);
else
*p=’@’;
}
puts(a);
}
void main()
{ int array[]={0,2,4,6};
int *pointer=array;
for(int i=1;i<=3;i++)
,cout<<*pointer<<”#”; pointer++;-
cout<<endl;
for(i=1;i<=4;i++)
{(*pointer)*=2;
Pointer--;}
for(i=1;i<5;i++)
cout<<array[i-1+<<”%”;
cout<<endl;
}

6. void main()
{
int a[ ]={20,40,60,80,100};
int *b[ ]={a,&a[2],&a[3]};
char *cp=”SCORE”;
cout<<cp<<”$”<<*b*0+<<”#”<<*b*1+<<”#”<<”\n”;
++**b;
*b[1]+=5;
cout<<*cp<<”$”<<*b*0+<<”#”<<*b*1+<<”#”<<”\n”;
}

7. int main()
{
int a = 32, *ptr = &a;
char ch = 'A', &cho = ch;
cho += a;
*ptr += ch;
cout << a << ", " << ch << endl;
return 0;
}

8. int main()
{ const int i = 20;
const int* const ptr = &i;
(*ptr)++;
int j = 15;
ptr = &j; cout << i; return 0;}
9. int main()
{ int num[5];
int* p;
p = num;
*p = 10;
p++;
*p = 20;
p = &num[2];
*p = 30;
p = num + 3;
*p = 40;
p = num;
*(p + 4) = 50;
for (int i = 0; i < 5; i++)
cout << num[i] << ", "; return 0;}

10. void main()


{ int arr[] = { 4, 5, 6, 7 };
int* p = (arr + 1);
cout << *arr + 10;
}

11. void main()


{ int a=5;
int *b=&a;
cout<<a<<’\t’<<*(&a)<<endl;
cout<<sizeof(a)<<’\t’<<sizeof(&a)<<endl;
cout<<*b<<endl;
cout<<sizeof(b)<<sizeof(*b)<<sizeof(&b);
}

12. void main()


, char a=’a’;
char *b=&a;
cout<<a<<’\t’<<*(&a)<<endl;
cout<<sizeof(a)<<’\t’<<sizeof(&a)<<endl;
cout<<*b<<endl;
cout<<sizeof(b)<<sizeof(*b)<<sizeof(&b);
}
13. void main()
{ int a[]={10,20,30,40};
int *p=a;
cout<<*a<<’\t’<<endl;
cout<<a+1<<’\t’<<*a+1<<endl;
cout<<sizeof(a)<<’\t’<<sizeof(*a)<<’\t’<<sizeof(&a)<<endl;
cout<<p<<’\t’<<*p<<’\t’<<*(p+1);
cout<<sizeof(p)<<’\t’<<sizeof(*p)<<sizeof(&p);
}
14. void main()
, char a*+=”pointer”;
char *p=a+3;
cout<<*a<<’\t’<<endl;
cout<<a+1<<’\t’<<(char)*a+1<<*(a+1)<<endl;
cout<<sizeof(a)<<’\t’<<sizeof(*a)<<’\t’<<sizeof(&a)<<endl;
cout<<p<<’\t’<<*p<<’\t’<<*(p+1)<<(char)(*p+1);
cout<<sizeof(p)<<’\t’<<sizeof(*p)<<sizeof(&p);
}
15. void main()
{
char *s=”Success”;
cout<<s++<<endl;
cout<<++s<<endl;
cout<<*s++<<endl;
cout<<*++s<<endl;
cout<<++*s<<endl;
cout<<s;
}
16. void main()
{
int arr[] = { 4, 5, 6, 7 };
int* p = arr;
for(int i=0;i<7;i++)
{cout<<*p+i<<endl;
p++;}
}
17. void fp(int *a,int n)
{for(int i=0;i<n;i++)
{*a+=i;
a++;
}
}
void main()
{
int x[]={3,5,7,9};
fp(x,4);
for(int i=0;i<4;i++)
cout<<x*i+<<”*”;
}
18. void main()
{char *s*+=,“Unary”,”Binary”,”Ternary”-;
char **p=s+2;
++p;
cout<<**p+1<<” “<<*(*p+1);
cout<<” “<<*(*p+1));
}
19. void main()
{int a=4,b=6,*c;
c=&a;
cout<<a**c*a+b;
}
20. void main()
,char s*20+=”Digital”;
char *const p=s;
*p=’A;
cout<<s;
}
B. Rewrite the following program after removing the syntax errors (if any). Underline the corrections:
1. typedef char[50] STRING;
void main()
{ int a={1,2,3,4};
int *p=&a;
City STRING;
cin.getline(city);
cout<< a*0+<<’\ t’<<City*2+;
}
2. #include <iostream.h>
struct square
{
int length, breadth;
}
void print(square *s)
{
cout << s.length<< “ ”<< s->breadth<<endl;
}
void main( )
{
SQUARE s1 = {5, 3 };
print(&s1);
square s2 = s1;
length.s2 ++;
}
3. #include<iostream.h>
void main()
{char *s=””;
int *p,n[]={2,4,6,8};
p=n;
cout<<*p<<T<<endl;
s++;
p++;
cout<<*p<<T<<endl;
}
4. #include<iostream.h>
void main()
{
int x[3],*y,z[3];
for(i=0;i<3;i++)
{x[i]=i;
Z[i]=i+3;
y=z;
x=y;
}
}

You might also like