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

Pointers Questions and Answers

21.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char *str;
    str = "%s";
    printf(str, "K\n");
    return 0;
}

 

Answer: C

Correct answer: K

printf takes its format as an ordinary const char * argument, so nothing requires it to be a literal written at the call site. Here str holds "%s", and printf(str, "K\n") uses it exactly as if the literal had been written there.

The output is K followed by a newline. The technique is legal but dangerous in practice: a format string taken from input rather than fixed in the source is the classic format string vulnerability, which is why compilers warn about non-literal formats.

Please log in or register to report this question.

22.

What will be the output of the program assuming that the array begins at location 1002?

#include <stdio.h>

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

 

Answer: C

Correct answer: 1002, 1002, 1002, 1

The first three arguments all denote the same address, differing only in type. a is a pointer to the first two-dimensional sub-array, *a a pointer to its first row, and **a a pointer to its first element - but the first element sits at the start of the first row, which sits at the start of the array, so all three are 1002.

Only ***a dereferences all the way through to a value, giving the first element, 1. This is the standard demonstration that an array's address and its first element's address coincide numerically while differing in type.

Please log in or register to report this question.

23.

The operator used to get value at address stored in a pointer variable is

Answer: A

Correct answer: *

The unary * is the indirection, or dereference, operator: it yields the object a pointer points to. Its counterpart & is the address-of operator, going the other way.

The asterisk plays two distinct roles that are easy to conflate. In a declaration it forms a pointer type; in an expression it dereferences. The && and || operators are logical AND and OR and have nothing to do with pointers.

Please log in or register to report this question.

24.

What is the output of this C code?

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

 

Answer: A

Correct answer: Compile time error

A void * cannot be dereferenced. void is an incomplete type with no size and no representation, so the compiler cannot know how many bytes to read or how to interpret them, and *p is rejected before the cast is even considered. The correct form casts the pointer first: *(int *)p.

Please log in or register to report this question.

Answer: B

Correct answer: We don't have address of an address operator

There is no && address operator in C. The token exists only as logical AND, so &&a is parsed as that operator with a missing left operand and the line will not compile. To obtain a pointer to a pointer you must have a pointer object to take the address of, as the earlier questions do with &p.

Please log in or register to report this question.

26.

What is the output of this C code?

int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int *j = (int*)malloc(sizeof(int));
*j = 10;
return j;
}

 

Answer: A

Correct answer: 10

The storage was obtained from malloc, so it lives on the heap and survives the return of the function that allocated it. The pointer therefore remains valid in main and dereferencing it gives 10. Compare the next question, where the same structure with an automatic variable is undefined - the difference is the storage duration, not the pointer.

Please log in or register to report this question.

27.

What will be the output of the following C code?

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

 

Answer: A

Correct answer: 11 11

In main, int *const p makes the pointer itself const, so &p has type int *const *. Passing that to a parameter of type int ** discards the qualifier, which is a constraint violation - but compilers generally warn rather than refuse, so the program runs.

foo then writes through the pointer, redirecting main's p to j, and both prints show 11. The exercise illustrates that const can be circumvented through a pointer conversion the compiler only warns about.

Please log in or register to report this question.

28.

Can you combine the following two statements into one?

char *p;
p = (char*) malloc(100);

 

Answer: C

Correct answer: char *p = (char*)malloc(100);

A declaration may carry an initialiser, so the declaration and the assignment combine directly: char *p = (char*)malloc(100);.

The other forms misplace the asterisks. (char)malloc(100) casts the returned pointer to a single character, and the last option is not valid syntax at all. It is worth adding that in C the cast is unnecessary - void * converts to any object pointer type implicitly, and omitting the cast can expose a missing stdlib.h that the cast would hide.

Please log in or register to report this question.

29.

What will be the output of the program if the size of pointer is 4-bytes?

#include <stdio.h>

int main()
{
    printf("%d, %d\n", sizeof(NULL), sizeof(""));
    return 0;
}

 

Answer: C

In TurboC, the output will be 2, 1 because the size of the pointer is 2 bytes in 16-bit platform.

But in Linux, the output will be 4, 1 because the size of the pointer is 4 bytes.

This difference is due to the platform dependency of C compiler.

Please log in or register to report this question.

30.

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.

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