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

Pointers Questions and Answers

21.

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

#include <stdio.h>

int main()
{
    int ***r, **q, *p, i=8;
    p = &i;
    q = &p;
    r = &q;
    printf("%d, %d, %d\n", *p, **q, ***r);
    return 0;
}

 

Answer: A

Correct answer: 8, 8, 8

Each level of indirection is undone by one dereference. p points to i, q points to p, and r points to q.

So *p reaches i directly; **q goes through p to i; and ***r goes through q and p to i. All three arrive at the same object and print 8. The distractors offer addresses, which would appear only if fewer dereferences were applied than there are levels of indirection.

Please log in or register to report this question.

22.

What is the output of this C code?

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

 

Answer: B

Correct answer: 11 11 Undefined-value

int **const p makes the parameter itself constant, but *p is not, so *p = &j is legal and the caller’s pointer is redirected to j. The first two prints give 11, one inside foo and one immediately after it returns. By the third, j’s lifetime has ended and the value is indeterminate - compare with the previous question, where the const sits in the other position and the code will not compile at all.

Please log in or register to report this question.

Answer: C

Correct answer: j is a pointer to an int and stores address of i

The declaration int *j = &i; declares j as a pointer to int and initialises it with the address of i.

The asterisk belongs to the declarator, not to the type specifier - the declaration reads "*j is an int", hence j is a pointer to int. i is an ordinary int and no pointer at all, and j has one level of indirection, not two, which the number of asterisks makes plain.

Please log in or register to report this question.

24.

What is the output of this C code?

void main()
{
int x = 0;
int *ptr = &5;
printf("%p\n", ptr);
}

 

Answer: D

Correct answer: Compile time error

The address-of operator requires an lvalue - something with a memory location - and the literal 5 has none. It is a value, not an object, so &5 is rejected outright. The same rule is why you cannot take the address of an arithmetic expression or of the result of a function call returning a value.

Please log in or register to report this question.

25.

What will be the output of the following C code?

#include <stdio.h>
    void foo(int*);
    int main()
    {
        int i = 10, *p = &i;
        foo(p++);
    }
    void foo(int *p)
    {
        printf("%d\n", *p);
    }

 

Answer: A

Correct answer: 10

Here the address is held in a pointer variable, so p++ is legal. It is a post-increment, so the value passed to foo is the original value of p - the address of i - and p is incremented afterwards.

foo therefore dereferences a pointer to i and prints 10. The increment of p has no effect on the output, and since p is never used again the value it now holds - one past i - is harmless.

Please log in or register to report this question.

26.

Which of the statements is correct about the program?

#include <stdio.h>

int main()
{
    float a=3.14;
    char *j;
    j = (char*)&a;
    printf("%d\n", *j);
    return 0;
}

 

Answer: A

Correct answer: It prints ASCII value of the binary number present in the first byte of a float variable a.

Casting &a to char * reinterprets the float's storage as bytes. Dereferencing reads the first byte of the IEEE representation of 3.14 - a piece of the mantissa, not the number 3 - and %d prints that byte's numeric value.

The output is therefore a small integer determined by the bit pattern and by the machine's endianness, with no arithmetic relationship to 3.14. The other options misdescribe it: %d prints a number rather than a character, and the value is deterministic rather than garbage.

Please log in or register to report this question.

27.

Are the expression *ptr++ and ++*ptr are same?

Answer: B

Correct answer: False

They differ in what is incremented. *ptr++ parses as *(ptr++), because the postfix ++ binds more tightly than the unary *: it yields the value currently pointed to and advances the pointer.

++*ptr parses as ++(*ptr): it dereferences first and increments the pointed-to value, leaving the pointer where it was. The first walks along an array reading it; the second modifies one element in place.

Please log in or register to report this question.

28.

In the following program add a statement in the function fun() such that address of a gets stored in j?

#include <stdio.h>
int main()
{
    int *j;
    void fun(int**);
    fun(&j);
    return 0;
}
void fun(int **k)
{
    int a=10;
    /* Add a statement here */
}

 

Answer: C

Correct answer: *k=&a

fun receives &j, so k points to the caller's pointer j. Writing through it - *k = &a - stores the address of a into j itself, which is what the question asks for.

The alternatives fail at different levels: k = &a changes only the local parameter, **k = a assigns a value rather than an address, and &k = *a is not valid syntax. Note that a is local to fun, so the address stored dangles once the function returns.

Please log in or register to report this question.

29.

The following program reports an error on compilation.

#include <stdio.h>
int main()
{
    float i=10, *j;
    void *k;
    k=&i;
    j=k;
    printf("%f\n", *j);
    return 0;
}

 

Answer: B

Correct answer: False

The program compiles cleanly. In C a void * converts to and from any object pointer type implicitly, so k = &i; and j = k; both need no cast, and the value printed is 10.000000. C++ is the language that would require an explicit cast on the second assignment; C deliberately does not.

Please log in or register to report this question.

Answer: A

Correct answer: Representation of NULL pointer

(void*)0 is an integer constant expression of value zero cast to void *, which is precisely the definition of a null pointer constant. The NULL macro is typically defined as exactly this.

It is not merely "a void pointer" - the value matters as much as the type. Note also that the null pointer's internal representation need not be all-zero bits on every machine; what the standard guarantees is that this constant converts to a null pointer of any type.

Please log in or register to report this question.

Loading…

Other Subjects

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