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

Pointers Questions and Answers

91.

What will be the output of the program ?

#include <stdio.h>

void fun(void *p);
int i;

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

 

Answer: D

Correct answer: 0

i is a global, so it has static storage duration and is zero-initialised before the program starts.

Inside fun, &p is the address of the parameter, which itself holds &i. Casting that to int ** and applying ** therefore reads first the parameter, obtaining &i, and then i itself - giving 0. The double cast is a roundabout way of reaching a value that *(int*)p would have given directly.

Please log in or register to report this question.

92.

What is the output of this C code?

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

Here the address of the pointer is passed, so *p = &j modifies the caller’s pointer itself and both prints give 2. That is how a function changes a pointer in its caller. Note that it is also a bug in the making: after foo returns, p in main points at j, whose lifetime has ended, so the second dereference is strictly undefined even though it prints 2 in practice.

Please log in or register to report this question.

Answer: C

Correct answer: A variable that stores address of other variable

A pointer is a variable whose value is the address of another object. It has a type - the type of what it points to - which determines both how the pointed-to bytes are interpreted and how pointer arithmetic is scaled.

It is not a keyword; there is no pointer keyword in C, the asterisk in a declaration doing the work. And while a function pointer holds the address of code, the general definition is in terms of objects, so the option naming instructions describes a special case rather than the concept.

Please log in or register to report this question.

94.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str1[] = "India";
    char str2[] = "BIX";
    char *s1 = str1, *s2=str2;
    while(*s1++ = *s2++)
        printf("%s", str1);

    printf("\n");
    return 0;
}

 

Answer: B

Correct answer: BndiaBIdiaBIXia

The loop copies str2 into str1 one character at a time, printing the whole of str1 after each copy, and stops when the assigned value is the terminating null.

After copying B, str1 reads Bndia; after I, BIdia; after X, BIXia. The three prints concatenate to BndiaBIdiaBIXia. Note that the assignment's value is what controls the loop, which is the standard C string-copy idiom.

Please log in or register to report this question.

Answer: B

Correct answer: k is a pointer to a pointer to a pointer to a pointer to a char

Each asterisk adds one level of indirection, so four asterisks give four levels: k is a pointer to a pointer to a pointer to a pointer to char.

Counting the asterisks is the whole technique for reading such declarations. In practice more than two or three levels are rare outside argument-vector manipulation and certain data structures, and where they arise a typedef for the intermediate levels usually makes the code readable.

Please log in or register to report this question.

96.

What is the output of this C code?

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

 

Answer: B

Correct answer: Compile time error

int *const ptr declares a constant pointer to a modifiable int: the pointer itself may not be changed after initialisation. So ptr++ is an attempt to modify a const object and the compiler rejects it. Read the declaration from the name outward - const binds to what is on its left, so here it qualifies the pointer rather than the pointee.

Please log in or register to report this question.

97.

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, **p);
}

 

Answer: D

Correct answer: Compile time error

The last argument is **p, but p is an int *, so *p is an int and applying * to an integer is not allowed. The number of dereferences must match the number of levels the pointer actually has. Writing **m instead, as in the previous question, is the correct form and gives 5.

Please log in or register to report this question.

98.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char str[20] = "Hello";
    char *const p=str;
    *p='M';
    printf("%s\n", str);
    return 0;
}

 

Answer: A

Correct answer: Mello

char *const p makes the pointer const, not what it points to. So p cannot be made to point elsewhere, but *p = 'M' is perfectly legal and writes through it.

str is a modifiable array, so the write succeeds and the string becomes Mello. Compare const char *p, where the qualifier applies to the pointed-to characters and the assignment would be rejected - the position of const relative to the asterisk decides which is protected.

Please log in or register to report this question.

99.

What will be the output of the following C code?

#include <stdio.h>
    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 p itself const but leaves *p modifiable, so *p = &j is legal and main's pointer is redirected to j.

The first two prints show 11. But j is local to foo, and its lifetime ends when foo returns, so the pointer left in main is dangling. The second print in main reads that dead stack slot, which may still hold 11 or may not - it is undefined behaviour, and the intervening call to printf is exactly the sort of thing that overwrites it.

Please log in or register to report this question.

Answer: A

Correct answer: You cannot change the value pointed by ptr

The const applies to what ptr points at, not to ptr itself, so *ptr = 5 is rejected while ptr = &other is allowed.

Read such declarations from the identifier outwards, or apply the rule that const qualifies whatever lies to its left unless there is nothing there, in which case it qualifies what lies to its right. int * const ptr reverses the meaning - a constant pointer to a modifiable int - and const int * const ptr fixes both.

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