Machine-translated by Google. Questions are authored in English.
Top

Pointers Questions and Answers

61.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int x=30, *y, *z;
    y=&x; /* Assume address of x is 500 and integer is 4 byte size */
    z=y;
    *y++=*z++;
    x++;
    printf("x=%d, y=%d, z=%d\n", x, y, z);
    return 0;
}

 

Answer: D

Correct answer: x=31, y=504, z=504

*y++ = *z++ assigns *z to *y - writing 30 back over itself - and then increments both pointers, since ++ in the postfix position binds to the pointer rather than to the dereferenced value.

Pointer arithmetic is scaled by the pointed-to type, so each advances by sizeof(int), that is 4 bytes: from 500 to 504. x++ then makes x 31. Both pointers now point one past x and must not be dereferenced.

Please log in or register to report this question.

62.

What will be the output of the program ?

#include <stdio.h>
int *check(static int, static int);

int main()
{
    int *c;
    c = check(10, 20);
    printf("%d\n", c);
    return 0;
}
int *check(static int i, static int j)
{
    int *p, *q;
    p = &i;
    q = &j;
    if(i >= 45)
        return (p);
    else
        return (q);
}

 

Answer: D

Correct answer: Error: cannot use static for function parameters

The only storage class specifier permitted on a function parameter is register. Declaring a parameter static is a constraint violation, and the compiler rejects both the prototype and the definition.

The reason is that a parameter's storage duration is fixed by the call: it comes into existence when the function is entered and vanishes when it returns. static would demand storage lasting the whole program, which is incompatible with being a parameter at all.

Please log in or register to report this question.

63.

Point out the compile time error in the program given below.

#include <stdio.h>

int main()
{
    int *x;
    *x=100;
    return 0;
}

 

Answer: C

Correct answer: No error

The compiler has nothing to object to: x is a pointer to int and 100 is an int, so *x = 100 is well typed. The fault is at run time, not compile time - x was never given a value, so the assignment writes through an indeterminate address. This is the distinction the question is testing: a program can be perfectly legal and still crash.

Please log in or register to report this question.

64.

What is the output of this C code?

void main()
{
int a[3] = {1, 2, 3};
int *p = a;
int **r = &p;
printf("%p %p", *r, a);
}

 

Answer: C

Correct answer: Same address is printed.

r holds the address of p, so *r is p itself, and p was set from a, which decays to the address of its first element. So *r and a print the same address. Note that the levels are now consistent, unlike the previous question, which is why this one compiles.

Please log in or register to report this question.

65.

What will be the output of the program?

#include <stdio.h>

int main()
{
    int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
    int *p, *q;
    p = &arr[1][1][1];
    q = (int*) arr;
    printf("%d, %d\n", *p, *q);
    return 0;
}

 

Answer: A

Correct answer: 8, 10

The initialiser fills the three-dimensional array in row-major order, so the eight values map to arr[0][0][0] through arr[1][1][1] in sequence: 10, 2, 3, 4, 5, 6, 7, 8.

The last of them, 8, is at arr[1][1][1], which is where p points. And q, cast from the array name, points to the very first element, which holds 10. So the output is 8, 10.

Please log in or register to report this question.

66.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[] = "peace";
    char *s = str;
    printf("%s\n", s++ +3);
    return 0;
}

 

Answer: D

Correct answer: ce

The expression s++ +3 uses the original value of s, since ++ here is postfix, and adds 3 to it. s points at the start of "peace", so the argument to printf addresses position 3.

Counting from zero - p, e, a, c - position 3 is the c, so the output is ce. The increment of s itself happens too, but s is never used again. The spacing in s++ +3 is necessary: s+++3 would tokenise differently.

Please log in or register to report this question.

67.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        int i = 97, *p = &i;
        foo(&p);
        printf("%d ", *p);
        return 0;
    }
    void foo(int **p)
    {
        int j = 2;
        *p = &j;
        printf("%d ", **p);
    }

 

Answer: A

Correct answer: 2 2

foo now takes a int ** and is given &p, so it can reach the caller's pointer itself. Assigning *p = &j changes main's p to point at j, and both prints show 2.

There is a hazard concealed in the output, though: j is local to foo and its lifetime ends when foo returns, so main's p is left dangling. The second print happens to find 2 still in that stack location, but reading it is undefined behaviour.

Please log in or register to report this question.

68.

How many number of pointer (*) does C have against a pointer variable declaration?

Answer: D

Correct answer: No limits

The standard sets no limit on the number of levels of indirection, and a conforming implementation must support at least twelve pointer, array and function declarators modifying a type. In practice more than two or three levels is a sign that the design would be better served by a structure or a typedef, since the declarations become almost unreadable.

Please log in or register to report this question.

69.

What is the output of the code below?

int main()
{
int i = 10;
int *const p = &i;
foo(&p);
printf("%d\n", *p);
}
void foo(int **p)
{
int j = 11;
*p = &j;
printf("%d\n", **p);
}

 

Answer: A

Correct answer: 11 11

int *const p in main makes p a constant pointer, but the address of it is passed to a function taking a plain int **, which discards the qualifier. That is a constraint violation and a conforming compiler must diagnose it, though most treat it as a warning and compile. The code then modifies p through the unqualified pointer and both prints give 11 - which is precisely why the warning matters.

Please log in or register to report this question.

70.

If the size of integer is 4bytes, What will be the output of the program?

#include <stdio.h>

int main()
{
    int arr[] = {12, 13, 14, 15, 16};
    printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
    return 0;
}

 

Answer: B

Correct answer: 20, 4, 4

sizeof(arr) gives the size of the whole array - five elements of four bytes, so 20. It is one of the few contexts in which an array name does not decay to a pointer.

*arr is the first element, an int, so its size is 4; and arr[0] is the same object written differently, so it too is 4. The pair is worth noting because sizeof(arr)/sizeof(arr[0]) is the idiomatic way to count an array's elements - and it fails silently once the array has decayed to a pointer.

Please log in or register to report this question.

Loading…
Tags: Pointers Questions and Answers || Pointers MCQ Questions and Answers || Pointers GK Questions and Answers || Pointers GK MCQ Questions || Pointers Multiple Choice Questions and Answers || Pointers GK || GK on Pointers || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming