EXAMPLES ON POINTER

1. Find the output

void main()
{
    char *str="lakshya";
    printf("%.*s""%.*s""%.*c",
        1, str, 2,str,0,*str);
}

Ans: llal
Explanation: When *is  inserted within %s then it will print the string up to the number supplied as argument.
printf("%.*s""%.*s""%.*c",1,str,2,str,0,*str);
The first string will be printed up to one character; the second string will be printed up 10 two characters. There is no effect of .*in %c.


2.Find the output

float *call (int *p)
{
    p=p+1;
    return (p);
}
void main()
{
    char *ptr="missing link";
    ptr=(double*)call ((char*)ptr);
    ptr++;
    printf("%s",ptr);
}

Ans: "sing link"
Explanation: ptr is pointer to character but p is a pointer to an integer. p+1 in call function increments the address ptr next two memory cell and ptr is again incremented into next memory cell in main function, so finally printf points "sing link"

3.Find the output

int main (int argc, char **argv)
{
    int *a=malloc(size of (int));
    int *b=malloc(size of (int));
    int *c=malloc(size of (int));
    *a=6;
    *b=2;
    *c=*a/*b;
    printf("*a=%d,*b=%d,
        *c=%d\n",*a,*b,*c);
}

Explanation: Here in the statement *c=*a/*b we are expecting *a is divided by *b, but it is not happending, because in C the comment line started with /* so it become error "unexpected end of comment line".

4.Find the output

void main()
{
    char *s[]={"miller","miller",
        "miller","miller"};
    char **p;
    p=s;
    printf("%s",++*p);
    printf("%s",*p++);
    printf("%s",++*p);
}

Ans: illerilleriller
Here s is an array of pointers.
s[0] contains the string address of first string miller.
s[1] contains the string address of second string miller.
s[2] contains the string address of third string filler.
s[3] containd the string address of fourth string miller.
p is pointing to the base address of the array s.
.  ++*p - ++ and *both have same precedence and associativity is from right to left so first
*p which is the address of the first string. The address is incremented.
.  *p++- as ++ is post increment the address will be incremented in next step. So the output will be same as previous case.
.  ++*p-now p contains the address of s[1]. *p is the address of the string miller. The address is incremented and s[0] contains the incremented address or the address of i.

5.Find the output

void main()
{
    int j=97,i=65;
    char *p="%d%c";
    printf((int*)p+1,i,j);
}

Ans: A
Explanation: Here p points to base address of string "%d%c" and in printf statement (int*)p+1, p is shifted to next two byte "%c". so finally printf is printf("%c",i), So the output is A

6.Find the output

void func(int *x)
{
    x=(int*)malloc(sizeof (int));
    printf("in func: %d\n",x);
}
void main(int arqc)
{
    int **pp;
    int  *p;
    pp=(int**)malloc(size of (int*));
    p=(int*)malloc(sizeof (int));
    *pp=p;
    printf("first: %d\n",*pp);
    func(*pp);
    printf("last: %d\n",*pp);
}

Assume that p is equal to 1000 and x is equal to 2000 after malloc calls

Ans: 1000 2000 and 1000
Explanation: Here in the above program *p and **p is 1000 according to above statement and 2000 is assigned to x.So the output will be 1000 2000 and 1000.

7.Find the output

void main()
{
int far *farther,*farthest;
printf("%d %d",sizeof (farther||farthest),
    sizeof(farthest &&farther) );
}

Ans: 22
Explanation: The return value of logical operator || or && is an integers,thus sizeof returns 2

8.Find the output

void main()
{
    char *p;
    p="Hello";
    printf("%c",*(&(*(p+1))+1)+1);
}

Ans: m
Explanation: Here first *(p+1) is 'e'and & (e)+1 is address of '1' then *(address of 1)+1 is m. so the output is m

9.Find the output

void main()
{
    char *s="\0";
    if (strcmp(s,"\0")==0)
    printf("s is null");
    else
    pritnf("s is not null");
}

Ans: s is null
Explanation: stremp retuns 0 if both strings are same. So in the program both string are same, thus condition is true and output is s is null

10.What is the problem in the following code.

void main()
{
    int *j,a=5;
    void *k;
    j=k=&a;
    j++;
    k++;
    printf("\n%d %d",j,k);
}

Explanation: Arithmetic is not allowed in generic pointer. So in the above program k++ is illegal and causes an error

11.What is the problem in the following program

main()
{
    char *p;
    *p=90;
    printf("%d",*p);
}

Explanation: Here p is initialize pointer called as wild pointer. If 90 will be placed in unknown memory location it can be segmentation fault.

12.Give the Right-left associativity of the following function declaration

int(*(*p())())[5];

Explanation: int (*(*p())())[5];
        5 3 1 2 4 6
Here p is a function returning address of another function which is returning address of an array of integers.

13. What is the problem in the following statement?

main()
{
    void *p="lakshya";
    *p='m';
    printf("%s",p);
}

Explanation: A generic pointer can not be dereferenced, so it causes an error.

14.What is the output

void main()
{
    char far *p=(char far *)0x50043002;
    char far *q=(char far *)0x51022022;
    *p=90;
    *q=10;
    printf("%d",*p);
}

Ans: 90
Explanation: The real address of p and q is same so 90 will be place in p means now the value of q is 90 and output is 90.

15. Write the missing statement exculuding x so that the output will be 50?

void main()
{
    int x=50;
    int const *p=&x-1;
    printf("%d",/*add here*/);
}

Explanation: Write *(p+1) in the printf statement, so the output will be 50

16. Write the missing statement so that the output will be ibj!gsjfoet

void main()
{
    char *p="hai friends",*p1;
    p1=p;
    while (*p!='\0')
    /*add here*/
    printf("%s %s",p,p1);
}

Explanation: *p=*p+1;p++ is the missing statement to get the output ibj!gsjfoet

17. Write the missing statement so that the output will be 0001 0002 0003?

void main()
{
    chat *p;
    int *q;
    long *r;
    p=q=r=0;
    p++;
    q++;
    r++;
    /*add here*/
}

Explanation: the missing statement is printf("%p %p %p",p,q,(char*)r-1);

18. find the output

void main()
{
    char *p="ayqm";
    printf("%c",*p++);
    printf("%c"++*(p++));
}

Ans: az
Explanation: Here *p++ in printf statement prints a and pointer is shifted to next address. In second line ++*(p++), the value 'y' is incremented to z so the output is az.

19. Write the missing statement so that the output will be 300?

void main()
{
    char *p;
    p="%d\n";
    /*add here*/
    pritnf(p-2,300);
}

Explanation: p=p+2 is the missing statement to get the output 300

20. Write the missing statement so that the output will be g%G.

void main()
{
    /*add here*/
    char *p="%%%c%%%c";
    printf((int *)p+1,a,b);
}

Explanation: Here the missing statement is char a='g',b='G';

21. Write the missing statement so that the output will be 16 16 16?

void main()
{
    int a=2,*f1,*f2;
    f1=f2=&a;
    /*add here*/
    printf("%d %d %d",a,*f1,*f2);
}

Explanation: Here the missing statement is a=16 to get the desired output.

22. Write the missing statement so that the output will be 1 1?

void main()
{
    /*add here*/
    int *itr=&i;
    printf("%d %d",*((char*)itr,
            *((char*)itr+1));
}

Explanation: Here the missing statement is int i=257; to get the output as 1 1.

23. What is the problem int the following code?

void main()
{
    int (*p)()=300;
    p++;
    printf("%d",p);
}

Explanation: Arithmetic is not allowed in pointer to function.

24. Write the missing statement so that the output will be 3 4 5 9?

int sum(char *,...);
void main()
{
    sum("total",3,4,5,9,0);
}
int sum(char *x,...)
{
    void *p=...;
    while(*p!=0)
    {
        printf("%d",*p);
    }
}

Explanation: here p is a generic pointer and arthmetic is not allowed to it. To do arithmetic with generic pointer type casting is necessary, such as in while(*(int*)p!=0) and in printf("%d",*((int*)p)++).

25. Define the following function.

int (*(*p)())[5];

Explanation: Here p is a pointer to function which returns address of an array.

26. Find the output

void main()
{
    void near *p;
    printf("%d",sizeof(p));
}

Ans: 2
Explanation: The sezi of near pointer is 2 byte, so the output is 2.

27.Find the putput

void main()
{
    char *p="abc";
    char *q="abc123"
    while(*p++=*q++)
        printf("%c%c",*p,*q);
}

Ans: bbcc 1a2b3c
Explanation: Here first *q is assigned to *p then check the condition whether *pvalue is zero of non-zero, then p and q is incremented so, the output is bbcc 1a2b3c.

28. Modify to get N and F in the output

void main()
{
    struct stud
    {
        char name[20];
        char *str;
    };
    struct stud s1={"Near pointer ","far pointer"},*s2;
    s2=&s1;
    printf("%c %c",s2->name,s2->str);
}
Explanation: Here s2-> name and s2->str gives the address of the string and %s is the suitable format specifier to print them using printf. But %c can't print the string, it just prints the character. So here the output is garbage.

29.Find the output

void main()
{
    float x,*y;
    x=12.5;
    +y=x;
    printf("%f",y);
}

Explanation: Here y does not point to any memory location. so if pointer is deference to unknown memory location that is called memory core dump.

30.Find the output

void main()
{
    int x=12,y=34,z;
    z=&y-&x;
    printf("%",z);
}

Ans: -1
Explanation: Here x,y and z are local automatic variables and created in stack area of process memory. Their addresses are in decreasing order. So address of y is smaller than address of x, so resulting x value is -1 because of types is integer.

31.find the output

int *p;
int weird(int *a)
{
    a=(int *)malloc(sizeof(int));
    *a=15;
    p=a;
}
int iered(int *bb)
{
    int b[10];
    *b=5;
    bb=b;
}
void main()
{
    int *a=p,*b;
    weird(a);
    iered(b);
    printf("%d %d",*a,*b);
}


Ans: 0 garbage
Explanation: Here a and b is a local to weid and iered function, so any modification to a and b that does not affect to other function. And in main function a is a pointer that points to p whose intitial value is 0. So the result is 0 and garbage.
   
32. What is the problem with the following code?

#define INTPTR int *
void main()
{
    INTPTR pi,pj;
    int i,j;
    i=10;j=20;
    pi=&j;
    pj=&j;
    j++;
    i=*pi;
    printf("%d ",i);
    j++;
    printf("%d",pj);
}

Explanation: Here pi is a pointer but pj is not a pointer. So pi can hold the address where as pj does not hold. So it is suspicious pointer conversion.

33. Find the output

typedef char (*arr_ptr)[];
int main(int argc, char  **argv)
{
    char string[]="Hello";
    arr_ptr ptr;
    ptr=&string;
    printf("%s\n",*ptr);
}

Ans: Hello
Explanation: Here ptr is a pointer to an array which holds the base address of an array.So *ptr gives the address of 0th element of array string. So the result is Hello.

34.Find the output.

#define MAX 10
int main()
{
    float a [MAX],*p1,*p2;
    p1=p2=a;
    while(p2!=&a[MAX])
    {
        printf("Defference:%d",
                (int)(p2-p1));
    p2++;
    }
}

Ans: 0 1 2 3 4 5 6 7 8 9
Explanation: Address-Address=number. So the above program prints the output as difference 0,difference 1 ...difference 9

35. Modify the program to get the output as TEST

#include<stdio.h>
void printme(char **p)
{
    printf("%s",*p);
}
int main(void)
{
    char s[]="TEST";
    char **p;
    p=(char **)&s;
    printme(p);
}

Explanation: Garbage output. As %s format specifier requires an address of a string, here if got a garbage address so prints garbage value present at that address.

36.Find the output.

void main()
{
    char far *p=(char far*)0x55550005;
    char far *q=(char far*)0x53332225;
    *p=25;
    (*p)++;
    printf("%d",*q);
}

Ans: 26
Explanation:The real address of p and q is same (segement *16+offset) that is 55555. So value of *p is same to *q, thus result is 26

37.Find the output.

void main()
{
    char arr[8]={'s','H','R','A','W','A','N'};
    char *p;
    p=(char*)(arr++2)[2]+1;
    printf("%c",p);
}

Ans: X
Explanation: array name +1=address of next element of an arry. Here in the above program (arr+2) gives the address of 'R' and (arr+2) [2] is 'W'. So p=(char*)(arr+2)[2]+1 is 'W'+1='X'.

38.Find the output.

void main()
{
    int x[6]={12.24,36,48,60,72};
    int *pi,*cpi=&x[5],i=0;
    pi=x;
    while(i<6)
    {
        if((*cpi%*pi))
        printf("NEVER\n");
        pi=cpi;
        pi++;
        if ((pi-cpi)!=72)
        printf("NEVER\n");
        i++;
    }
}

Ans: EVER EVER EVER EVER EVER NEVER EVER
Explanation: Here the loop will continue six times and first five last condition is true and last time both conditions are true.

39.Find the output.

#include<stdio.h>
void main()
{
    numcmp("123.45","56.6776");
}
numcmp(char *str1,char *str2)
{
    printf("%0.3f %0.3f",atof(str1),atof(str2+2));
}

Ans: 123.45 and 0.678
Explanation: Here the function atof converts a string into float.So the output is 123.45 and 0.678

40.Find the output.

main()
{
    int x;
    char *p=&x;
    *(p+0)=2;
    *(p+1)=1;
    printf("%d",x);
}

Ans: 258
Explanation: First byte data *256+second byte=the value of an integer variable.So here in the above example first bytes of x are 2 and second byte is 1 so data becomes 258 (if systemis little endianess type).

41.Find the output.

char *ptr[]={"Life:","a","jorney",
        "just","keep","going"};
char **ptr[]={ptr+5,ptr+4,ptr+3,
        ptr+2,ptr+1,ptr};
char ***ptr2=ptr1;
main()
{
    printf("%s",++**ptr2++);
    printf("%s",**++ptr2+2);
}

Ans: oingst
Explanation: Here in the first peintf ptr2 means 0th elements of ptr1 which is ptr+5 that is "going" and **ptr2 means its base address and ++**ptr2 becomes "oing" and in second printf statement ptr2 is points to "just" and**++prtr2+2 gives address of 3rd element of "just",so output is "st".

42.Find the output.

void main()
{
    char a[4]={'11','33','55','77'};
    char (*p)[4];
    p=&a;
    printf("%c %c",**p++,*(*p+1));
}

Ans: 1 3
Explanation: Here in printf statement first*(*p+1) gives the value of second element which is 3 and **p++ gives value of first element that is 1. Character constant can be two characters long but first character only can be considered in the above case.

43. Make correction to get the expected result

void main()
{
    struct
    {
        int num;
        float value;
        char name[30];
    }k;
    k.num=1;
    k.value=3.14;
    k.mess="kaumar"
    printf("\n %u %f %s",
            k,num,k,value,k.name);
}

Explanation: In the above program write strcpy(k.mess,"kumar") instead of k.mess"kumar";

44.Find the output.

void main()
{
    int a[3][3]={2,4,6,9,1,10,16,64,5};
    printf("%d\n",*(a[1]+1)|(a[1][2]));
    printf("%d\n",*(a[0]|*(a[2]));
    printf("%d",a[2][2]&a[2][0]);
    printf("5d",++**a+  --a[1][1]);
}

Ans: 111803
Explanation: The first printf prints*(a[1]+1)that is 1 ORed eith (a[1][2]) that is 10. So the output is 11. The second printf*(a[0]) is 2 Ored with *(a[2]) is 16,so the output is 18. The third printf a[2][2] is 5 AND with a[2][0] that is 16, so the output is 0. In 4th printf ++**a is 3 plus with -a[1][1] is 0,so the output is 3.

45.Find the output.

main()
{
    static char *s[]={"black","white","yellow","violet"};
    char **ptr[]={s+3,s+2,s+1,s},**p;
    p=*(ptr+1);
    printf("%s",*p);
}

Ans: yellow
Explanation: Here p is a pointer pointing to the second element of array ptr which is a string address and printf prints *p means base address of string "yellow" is provided, thus the output is "yellow".

46. Write the missing statement to overcome the dangling pointer

char *myFunction()
{
    char mem[]="Dangling pointer:A problem";
    return mem;
}

Explanation: When the control leaves the scope of a function, the automatic local variables are dead and value goes lost, only address remains constant in memory. so to overcome this problem static is the only solution. such as static char mem[]="Dangling pointer.A problem"

47.Make correction to get the output: madhumita

void main()
{
    char *p;
    P=(char*)malloc (20);
    *p="Mashumita";
    printf("%s",*p);
}

Explanation:Write the statement strcpy(p,"Madhumita") in place of *p="Madhumita";

48.Write the missing code to print value of My Array:

#define R_MAX 2
#define C_MAX 3
void main()
{
    int MyArray[R_Max][c_MAX]={1,2,3,4,5,6};
    MyFun(MyArray);
}
MyFun(int(*ptr)[C_MAX])
{
    register i;
}

Explanation: Here ptr is a pointer to an array. To print the value of entire array the following code is required

register i,j;
inr *p;
for (j=0;j<R_max;j++)
{
    p=ptr+j;
    for (i=0;i<c_MAX;i++)
    {
        printf("%d",*p++);
    }
}

49.How to call the function if function names are present in an array of function.

void main()
{
    int MyFun(),MyFact();
    int (*arr[])()={MyFun,MyFact};
    //code here
}
int MyFun()
{
    printf("Inside MyFun");
}
int MyFact()
{
    printf("May i help u");
}

Explanation: Here arr[0] contains MyFun and arr[1] contains MyFact.So to call both functions the following code is used
        *(p[0])();
        *(p[1])();

50.Find the output

#define "stdio.h"
char *fun(a,c)
register char *a,c;
{
    do
    {
        if (*a==c)
        return a;
    }while (*a++);
    return NULL;
}
void main()
{
    printf("%s",fun("123456",'3'));
}

Explanation: Here fun is a function returning an address of character. When the condition is true function fun returns a and if the condition is false function returns NULL. So according to give example the loop is terminated when *a==c values of *a is 3). So the output becomes 3456.

No comments:

Post a Comment