Thursday, December 25, 2008

Pointer Question with Answer in C Programming Language


Q. What will be output of following program ?

void main()

{

int a=320;

char *ptr;

ptr=(char *)&a;

clrscr();

printf("%d ",*ptr);

getch();

}

Output:64

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.

Memory representation of data type in detail.

Q.What will be output?

#include"stdio.h"

#include"conio.h"

void main()

{

void (*p)();

int (*q)();

int (*r)();

p=clrscr;

q=getch;

r=puts;

(*p)();

(*r)("cprogramminglanguage");

(*q)();

}

Output:

cprogramminglanguage

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 then can hold the address of such function.

How can we read complex pointer in easy way click here.

Q.What will be output?

void main()

{

int i=3;

int *j;

int **k;

j=&i;

k=&j;

printf(“%u %u %d ”,k,*k,**k);

}

Answer:

Output: 8085 6024 3

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 Same way **k will 3.

Short cut way to calculate:

Rule : * and & alaway 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

To know concept of pointer click me

Q.What will be output?

void main()

{

char far *p=(char far *)0x55550005;

char far *q=(char far *)0x53332225;

clrscr();

*p=80;

(*p)++;

printf("%d",*q);

getch();

}

Output: 81

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


Q.What will be output of following program?

#include"stdio.h"

#include"string.h"

void main()

{

char *ptr1=NULL;

char *ptr2=0;

strcpy(ptr1," c");

strcpy(ptr2,"questions");

clrscr();

printf("\n%s %s",ptr1,ptr2);

getch();

}

Output :( null) (null)

Explanation: we cannot assign any string constant in null pointer

By strcpy function.


Q.What will be output?

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();

}

Output: power of pointer

Explanation:

Here we are performing relational operation between two huge addresses. So first both a and b will normalize.

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.

What is huge pointer in detail?

Q.What will be output of following program?

#include"stdio.h"

#include"string.h"

void main()

{

register a=25;

int far *p;

p=&a;

clrscr();

printf("%d ",*p);

getch();

}

output :error

Explanation: register data type stores in CPU. So it has not any

memory address. Hence we cannot write &a.

Q. What will be output of following program?

#include"stdio.h"

#include"string.h"

void main()

{

char far *p,*q;

clrscr();

printf("%d %d",sizeof(p),sizeof(q));

getch();

}

Output: 4 2

Explanation: p is far pointer which size is 4 byte.

By default q is near pointer which size is 2 byte.

Q.What will be output of following program?

void main()

{

int a=10;

void *p=&a;

int *ptr=p;

clrscr();

printf("%u",*ptr);

getch();

}

Output: 10

Explanation: void pointer can hold address of any data type without

type casting. Any pointer can hold void pointer without type casting.

What is generic pointer?

Q.What will be output of following program?

#include"stdio.h"

#include"string.h"

void main()

{

int register a;

clrscr();

scanf("%d",&a);

printf("%d",a);

getch();

}

//if a=25

Output: error

Explanation: register data type stores in CPU. So it has not any

memory address. Hence we cannot write &a.

Q. What will be output of following program?

void main()

{

char arr[10];

arr="world";

clrscr();

printf("%s",arr);

getch();

}

Output: compiler error Lvalue required

Explanation: Actual declaration of any array is:

Char const arr[10];

So array name is constant pointer and we cannot assign any value

in constant data type after declaration.

Concept of array

Q.Without using sizof operator find out size of following data type.

(1)char

(2)int

(3)float

(4)double

Answer:

#include"stdio.h"

#include"string.h"

void main()

{

int a,b,c,d;

char *p=0;

int *q=0;

float *r=0;

double *s=0;

a=(int)(p+1);

b=(int)(q+1);

c=(int)(r+1);

d=(int)(s+1);

clrscr();

printf("%d %d %d %d",a,b,c,d);

getch();

}

Output: 1 2 4 8

Explanation: address=next address

Since initial address of all data type is zero. So its next address will

be size of data type.

Q. What will be output of following program?

#include"stdio.h"

#include"string.h"

void main()

{

int a=5,b=10,c;

int *p=&a,*q=&b;

c=p-q;

clrscr();

printf("%d",c);

getch();

}

output :1

Explanation:Difference of two same type of pointer will always one.

Q.What will be output?

unsigned long int (* avg())[3]

{

static unsigned long int arr[3]={1,2,3};

return &arr;

}

void main()

{

unsigned long int (*ptr)[3];

ptr=avg();

clrscr();

printf("%d",*(*ptr+2));

getch();

}

Output: 3

Explanation: How can we read complex pointer in easy way click

No comments:

Blog List