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

Pointers Questions and Answers

31.

Which of the following statements correctly declare a function that receives a pointer to pointer to a pointer to a float and returns a pointer to a pointer to a pointer to a pointer to a float?

Answer: D

Correct answer: float ****fun(float***);

Read the declaration outward from the name. The parameter is a pointer to a pointer to a pointer to a float, so it is written float ***. The return type is one level deeper again, so it is float ****.

Count the asterisks against the words: three for the parameter, four for the return. The other options mismatch one or both. Where such declarations become unreadable, a typedef for the intermediate types is the usual remedy.

Please log in or register to report this question.

Answer: C

Correct answer: The value stored at the address held by p

The asterisk is the dereference operator, which follows the pointer to the object it refers to. The address of p itself is obtained with &p, and p alone gives the address it holds.

Please log in or register to report this question.

33.

What is the output of this C code?

int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}

 

Answer: D

Correct answer: 11,11

ptr holds the address of a, so *ptr += 1 increments a itself - there is only one object, reached by two names. Both print as 11. Note also the /n in the format string, which is a forward slash and the letter n rather than an escape, so it appears literally instead of starting a new line.

Please log in or register to report this question.

34.

What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?

#include <stdio.h>

int main()
{
    int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
    return 0;
}

 

Answer: C

Correct answer: 1006, 2, 2

Take the three arguments in turn. a[0] is a pointer to the first row's first element, at 1002, and adding 1 advances it by sizeof(int), giving 1006.

*(a[0]+1) dereferences that, yielding a[0][1], which is 2. And *(*(a+0)+1) is the same expression written with explicit pointer arithmetic - a+0 is a, *(a+0) is a[0] - so it too gives 2.

Please log in or register to report this question.

35.

What will be the output of the following C code?

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

 

Answer: C

Correct answer: Compile time error

&i yields the address of i, but it is a value, not an object - there is no storage holding it that could be incremented.

++ requires a modifiable lvalue, so (&i)++ is a constraint violation and the compiler rejects it. The contrast with the next case is instructive: if the address is first stored in a pointer variable, that variable is an object and can be incremented.

Please log in or register to report this question.

36.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char *p;
    p="hello";
    printf("%s\n", *&*&p);
    return 0;
}

 

Answer: B

Correct answer: hello

The operators * and & are inverses, so each adjacent pair cancels. Reading *&*&p from the right: &p gives the address of p, * takes us back to p, & takes the address again, and the final * returns to p.

The whole expression is therefore just p, and printf prints hello. Any even number of such pairs behaves the same way; the exercise exists to make the inverse relationship explicit.

Please log in or register to report this question.

37.

In which header file is the NULL macro defined?

Answer: C

Correct answer: stdio.h and stddef.h

NULL is defined in several headers, among them stdio.h and stddef.h, and also stdlib.h, string.h, time.h and others.

The duplication is deliberate. A programmer using any of those libraries is likely to need NULL, and the standard permits a macro to be defined identically in more than one header, so each defines it rather than forcing an extra include. stddef.h is the canonical home, holding it alongside size_t and ptrdiff_t.

Please log in or register to report this question.

38.

What is the output of this C code?

void foo(float *);
int main()
{
int i = 10, *p = &i;
foo(&i);
}
void foo(float *p)
{
printf("%f\n", *p);
}

 

Answer: B

Correct answer: 0.000000

The four bytes holding the integer 10 are read as an IEEE single-precision float, and that bit pattern is a denormal of about \( 1.4 \times 10^{-44} \), which %f prints as 0.000000. The pointer types are incompatible, so a conforming compiler must issue a diagnostic; older compilers treat it as a warning and produce this output, which is what the question is asking for.

Please log in or register to report this question.

39.

What is the output of this C code?

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

 

Answer: A

Correct answer: 0xbfd605e8 0xbfd605ec

Pointer arithmetic is in units of the pointed-to type, so incrementing an int * advances the address by sizeof(int), four bytes on a typical implementation. The second address is therefore four higher than the first, which the option with the last digit changing from 8 to c reflects. The particular addresses vary from run to run; the difference of four does not.

Please log in or register to report this question.

40.

What will be the output of the following C code?

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

 

Answer: A

Correct answer: 2 97

The pointer is passed by value, so foo receives a copy. Assigning p = &j inside foo changes only that copy, and the print there shows 2.

Back in main, the original p still points to i, so the second print shows 97. This is the fundamental point about pointer arguments: passing a pointer lets a function modify what it points to, but not the caller's pointer itself. Changing the caller's pointer requires a pointer to 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