Thursday, December 25, 2008

POINTER in C Program QUESTIONS With Explanation

Pointer Questions with Answers

Q1.What will output:
void main()
{
int i=3;
int *j;
int **k;
j=&i;
k=&j;
printf(“%u %u %u”,i,j,k);
}
Ans:


Here 6024 ,8085,9091 is any arbitrary address,it may be different.
Since contiant of i is 3,so first output will 3
Contiant of j is 6024 ,so second output will 6024
Containt of k is 8085 ,so third output will 9091
Output: 3 6024 8085

Q2.What will output :

Void main()
{
int * p,b;
b=sizeof(p);
printf(“%d”,b);
}
Ans:
Output: 2 or 4
Explanation:
Since in this question it has not written p is which type pointer. So it’s output will depends upon which memory model has selected. Default memory model is small.
More detail click here(pointer c)

Question of pointer in c programming
Q3.What will be output ?
void main()
{
int i=5,j;
int *p,*q;
p=&i;
q=&j;
j=5;
clrscr();
printf("value of i : %d value of j : %d",*p,*q);
getch();
}
output :5 5

Q4.What will be output ?
void main()
{
int i=5;
int *p;
p=&i;
clrscr();
printf(" %u %u",*&p,&*p);
getch();
}
output :65508 65508 (any address)
Explantin: since * and & alaway cancel to each other.
i.e *&a=a
so *&p=p which store address of integer i
&*p=&*(&i) //since p=&i
=&(*&i)
=&i so second output is also address of i

Q5.What will be output ?
void main()
{
int i=100;
clrscr();
printf("value of i : %d addresss of i : %u",i,&i);
i++;
printf("\nvalue of i : %d addresss of i : %u",i,&i);
getch();
}
output :
value of i : 100 addresss of i :65492
value of i : 101 addresss of i : 65492
Explanation: Within scope of any variable value of variable
may change but its address will never change in any modification of
variable.





Far pointer questions with solution
Q6. What will be output ?
void main()
{
char far *p=(char far *)0x55550005;
char far *q=(char far *)0x53332225;
clrscr();
*p=25;
(*p)++;
printf("%d",*q);
getch();
}
Output: 26
Explanation:
Far address of p and q are representing same physical address .
Physical address of 0x55550005= 0x5555*ox10+ox0005= 0x55555
Physical address of 0x53332225=0x5333*0x10+ox2225=0x55555
*p =25, means content at memory location 0x55555 is assigning value 25
(*p)++ means increase the content by one at memory location 0x5555 so now content at memory location 0x55555 is 26
*q also means content at memory location 0x55555 which is 26
(10) what will be output ?

#include
void main()
{
unsigned int i,offset_addr,seg_addr,;
char far *ptr=(char far *) 5555 FF99;
for (i=0;i<300;i++) seg_addr="FP_SEG(p);" offset_addr="FP_OFF(p);">

No comments:

Blog List