Practice Questions of Pointers
Practice Questions of Pointers
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++;
}
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;}