## Saturday, January 28, 2012

### puzzle code of pointer

Q1) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
int a=10,*p,**q,***r;
p=&a;
q=&p;
r=&q;
printf("%d",*(*(*r))+5);
getch();
}

Ans:-15

Q2) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
int a[][3]={
{1,2,3},
{4,5,6},
{7,8,9}
};
printf("%d",  *(*(a+1)+1));
getch();
}
Ans:-5
Discussion:-

Q3) Output of the program
#include "stdio.h"
#include "conio.h"
void print()
{
printf("sourav");
}

main()
{
void (*p)();    //Pointer of function declaration.
p=print;          //Function address assignment to pointer.
p();                 //Function call.
getch();
}
Ans:-sourav
Discussion:-Here p is a pointer of function print(); And the print() is called by p().

Q4)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
int a[2][2][2]={1,2,3,4,5,6,7,8};
printf("%d ",   *(*(*(a+1)+1)+1));
printf("%d ",***a);
getch();
}
Ans:-8 1
Discussion:-here a is a three dimension array. The first printf statement will print 8,the expression is equivalent to a[2][2][2] and second printf statement will print the a[0][0][0] th value of array a.

Q5)Is the code Wright.
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
main()
{
void *p;
p=malloc(100);
getch();
}
Ans:-No
Discussion:-To correct to coding it is need to cast the pointer.  The correct code is p=(char*)malloc(100);

Q6) Output of the program.
#include "stdio.h"
#include "conio.h"
struct abc
{
int a,b;
};
struct abc t,*p;
main()
{
p=&t;
p->a=10;
p->b=20;
printf("%d %d",p->a,p->b);
getch();
}
Ans:-10 20
Discussion:-Here p is a pointer of structure. p takes the address of a structure variable .and the arrow operator(->) is used to access the member of the structure.

Q7)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
char *p="%d";
printf(p,500);
getch();
}
Ans:-500
Discussion:-Here p is a character pointer. And hold the base address of the string “%d”.
In printf function p pointer replace by the string  “%d”.
Q8)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
int a[]={10,20};
int *p=a;
*p++;
printf("%d ",*p);
++*p;
printf("%d ",*p);
getch();
}
Ans:-20 21
Discussion:-The statement *p++ increment the content of p pointer, so it will point the second element of array a. And the statement ++*p will increment the content of p pointer.

Q9)Output of the program.
#include "stdio.h"
#include "conio.h"
#include "string.h"
main()
{
char *p="sourav";
char *q="kayal";
char a[20];
int k=0;
while(*p!=NULL)
{
a[k]=*p;
p++;
k++;
}
while(*q!=NULL)
{
a[k]=*q;
q++;
k++;
}
puts(a);
getch();
}
Ans:-souravkayal
Discussion:-This is the program of string concatenation using pointer. Here scale factor of character pointer is 1.
Q10) output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
printf("%d %d",sizeof(NULL),sizeof(""));
getch();
}
Ans:-It depends upon compiler.
In xp TC++ environment it prints 2 1
In window7 , DEV C++ environment it prints 4 1

Q11)Output of the program
#include "stdio.h"
#include "conio.h"
main()
{
int *p;
char *q;
float *r;
printf("%d %d %d",sizeof(p),sizeof(q),sizeof(r));
getch();
}
Ans:-Output is compiler dependent
Xp and TC combination gives 2 2 2
Window7  and DEV C++ gives 4 4 4.
Discussion:-It depends upon system memory byte size.

Q12)Find out the error.
#include "stdio.h"
#include "conio.h"
main()
{
int *p;
*p=100;
printf("%d",*p);
getch();
}
Ans:-The program is syntactically correct. But no output will come.
Discussion:-Here p is a integer pointer but not initialize by and address. So in run time it will take random address.

Q13)Output of program.
#include "stdio.h"
#include "conio.h"
main()
{

struct abc
{
int a;
int b;
};
struct abc t,*p;
t.a=100;
t.b=200;
printf("%d",sizeof(p));
getch();
}
Ans:-Output depends upon platform.
In XP and TC++ it will give 2
In Window 7 DEV C++ it will give 4