Do something to improve things. Anything. Start small or go big. Just do something. Today. Now. The world will be better for it. ----Just because no one else believes in your dreams doesn't mean you shouldn't believe in your dreams. ----The world is the great gymnasium where we come to make ourselves strong. "Swami Vivekananda"

Government Jobs In India Headline Animator

14 July 2010

C POINTER QUESTIONS

1.
What will be output of following program?

#include
void main(){
int a = 320;
char *ptr;
ptr =( char *)&a;
printf("%d ",*ptr);
getch();
}
(A) 2
(B) 320
(C) 64

(D) Compilation error
(E) None of above

Explanation:
As we know int is two byte data byte while char is one byte data byte. char pointer can keep the address one byte at time.
Binary value of 320 is 00000001 01000000 (In 16 bit)
Memory representation of int a = 320 is:



So ptr is pointing only first 8 bit which color is green and Decimal value is 64.

2.
What will be output of following program?

#include
#include
void main(){
void (*p)();
int (*q)();
int (*r)();
p = clrscr;
q = getch;
r = puts;
(*p)();
(*r)("cquestionbank.blogspot.com");
(*q)();
}

(A) NULL

(B) cquestionbank.blogspot.com

(C) c
(D) Compilation error

(E) None of above


Explanation:
p is pointer to function whose parameter is void and return type is also void. r and q is pointer to function whose parameter is void and return type is int . So they can hold the address of such function.

3.
What will be output of following program?

#include
void main(){
int i = 3;
int *j;
int **k;
j=&i;
k=&j;
printf(“%u %u %d ”,k,*k,**k);
}
(A) Address, Address, 3
(B) Address, 3, 3
(C) 3, 3, 3
(D) Compilation error
(E) None of above


Explanation:
Memory representation


Here 6024, 8085, 9091 is any arbitrary address, it may be different.
Value of k is content of k in memory which is 8085
Value of *k means content of memory location which address k keeps.
k keeps address 8085 .
Content of at memory location 8085 is 6024
In the same way **k will equal to 3.
Short cut way to calculate:
Rule: * and & always cancel to each other
i.e. *&a = a
So *k = *(&j) since k = &j
*&j = j = 6024
And
**k = **(&j) = *(*&j) = *j = *(&i) = *&i = i = 3

4.
What will be output of following program?

#include
void main(){
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 80;
(*p)++;
printf("%d",*q);
getch();
}
(A) 80
(B) 81

(C) 82
(D) Compilation error

(E) None of above

Explanation:
Far address of p and q are representing same physical address.
Physical address of 0x55550005 = (0x5555) * (0x10) + (0x0005) = 0x55555
Physical address of 0x53332225 = (0x5333 * 0x10) + (0x2225) = 0x55555
*p = 80, 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 81
*q also means content at memory location 0x55555 which is 26

5.
What will be output of following program?

#include
#include
void main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
getch();
}
(A) c questions
(B) c (null)
(C) (null) (null)
(D) Compilation error
(E) None of above


Explanation:
We cannot assign any string constant in null pointer by strcpy function.

6.
What will be output of following program?

#include
void main(){
int huge *a =(int huge *)0x59990005;
int huge *b =(int huge *)0x59980015;
if(a == b)
printf("power of pointer");
else
printf("power of c");
getch();
}
(A) power of pointer

(B) power of c
(C) power of cpower of c
(D) Compilation error

(E) None of above

Explanation:
Here we are performing relational operation between two huge addresses. So first of all both a and b will normalize as:
a= (0x5999)* (0x10) + (0x0005) =0x9990+0x0005=0x9995
b= (0x5998)* (0x10) + (0x0015) =0x9980+0x0015=0x9995
Here both huge addresses are representing same physical address. So a==b is true.

7.
What will be output of following program?

#include
#include
void main(){
register a = 25;
int far *p;
p=&a;
printf("%d ",*p);
getch();
}
(A) 25
(B) 4
(C) Address
(D) Compilation error
(E) None of above


Explanation:
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

8.
What will be output of following program?

#include
#include
void main(){
char far *p,*q;
printf("%d %d",sizeof(p),sizeof(q));
getch();
}
(A) 2 2
(B) 4 4

(C) 4 2

(D) 2 4

(E) None of above

Explanation:
p is far pointer which size is 4 byte.
By default q is near pointer which size is 2 byte.

9.
What will be output of following program?

#include
void main(){
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
getch();
}
(A) 10
(B) Address

(C) 2
(D) Compilation error
(E) None of above


Explanation:
Void pointer can hold address of any data type without type casting. Any pointer can hold void pointer without type casting.

10.
What will be output of following program?

#include
#include
void main(){
int register a;
scanf("%d",&a);
printf("%d",a);
getch();
}
//if a=25

(A) 25
(B) Address

(C) 0
(D) Compilation error

(E) None of above

Explanation:
Register data type stores in CPU. So it has not any memory address. Hence we cannot write &a.

No comments:

Subscribe via email

Enter your email address:

Delivered by FeedBurner

website-hit-counters.com
Provided by website-hit-counters.com site.