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

Pointers Questions and Answers

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.

32.

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.

33.

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.

34.

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.

35.

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.

36.

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.

37.

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.

38.

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.

39.

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.

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