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

Pointers Questions and Answers

11.

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.

12.

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.

13.

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.

14.

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.

15.

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.

17.

What will be the output of the program ?

#include <stdio.h>
#include <string.h>

int main()
{
    int i, n;
    char *x="Alice";
    n = strlen(x);
    *x = x[n];
    for(i=0; i<=n; i++)
    {
        printf("%s ", x);
        x++;
    }
    printf("\n", x);
    return 0;
}

 

Answer: D

Correct answer: lice ice ce e

x[n] is the terminating null, and *x = x[n] writes it over the first character, so the string becomes an empty string followed by lice.

The first iteration therefore prints nothing before its space. Each subsequent iteration advances x by one and prints the remaining tail - lice, ice, ce, e. Note that x points to a string literal, so writing through it is undefined behaviour and would fault on many modern systems.

Please log in or register to report this question.

18.

Which of the following does not initialize ptr to null (assuming variable declaration of a as int a=0)?

Answer: A

Correct answer: int *ptr = &a;

That initialiser gives ptr the address of a, which is a real object and therefore not a null pointer.

The other two do yield null. &a - &a is zero, and a - a is zero because a is 0 - and a constant expression evaluating to 0 is a null pointer constant, so it converts. Both are legal but poor style: NULL states the intention, and a subtraction that happens to give zero conceals it.

Please log in or register to report this question.

19.

What is the output of this C code?

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

 

Answer: C

Correct answer: 6

**m reaches all the way through to k: m points at p, and p points at k. So the assignment writes 6 into k itself, and printing k confirms it. This is how a double pointer lets code several levels away modify the original object.

Please log in or register to report this question.

20.

What is the output of this C code?

int main()
{
int a = 1, b = 2, c = 3;
int *ptr1 = &a, *ptr2 = &b, *ptr3 = &c;
int **sptr = &ptr1; //-Ref
*sptr = ptr2;
}

 

Answer: B

Correct answer: ptr1 points to b

sptr holds the address of ptr1, so *sptr is ptr1, and assigning ptr2 to it copies the address of b into ptr1. Afterwards ptr1 and ptr2 both point at b. Note that sptr itself is unchanged and still points at ptr1.

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