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

Pointers Questions and Answers

1.

What is the output of this C code?

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

 

Answer: A

Correct answer: 5 5 5

Each dereference removes one level. k is 5; p holds the address of k, so *p is 5; and m holds the address of p, so *m is p and **m is 5. All three names reach the same object by paths of different length, which is the whole point of the double pointer.

Please log in or register to report this question.

2.

What would be the equivalent pointer expression for referring the array element a[i][j][k][l]

Answer: B

Correct answer: *(*(*(*(a+i)+j)+k)+l)

Each subscript is defined as a dereference of a sum: x[y] means *(x+y). Applying that rule four times, from the outside in, gives one dereference for every subscript.

\[ a[i][j][k][l] \equiv *(*(*(*(a+i)+j)+k)+l) \]

The options without the asterisks compute addresses but never read through them. Note that the pointer arithmetic is scaled by the size of the sub-array at each level, which is why the array's dimensions must be known to the compiler.

Please log in or register to report this question.

3.

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.

4.

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.

6.

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.

7.

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.

8.

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.

9.

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.

10.

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.

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