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

Pointers Questions and Answers

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.

82.

Which is an indirection operator among the following?

Answer: B

Correct answer: *

The unary * is the indirection or dereference operator: applied to a pointer it yields the object addressed.

& is its inverse, the address-of operator, producing a pointer from an object. The other two select members - . from a structure and -> from a pointer to one - and -> is in fact indirection and selection combined, p->m being shorthand for (*p).m.

Please log in or register to report this question.

83.

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.

84.

What is the output of this C code?
 

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

 

Answer: A

Correct answer: Same address

Incrementing x changes the value stored at the address, not the address itself, and ptr was never modified. So both %p conversions print the same thing. The question is testing that a pointer and the object it points to are separate: assigning through the pointer alters one, assigning to the pointer alters the other.

Please log in or register to report this question.

85.

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.

86.

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.

87.

What is the output of this C code?

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

 

Answer: D

Correct answer: Compile time error

The address-of operator requires an lvalue - something with a memory location - and the literal 5 has none. It is a value, not an object, so &5 is rejected outright. The same rule is why you cannot take the address of an arithmetic expression or of the result of a function call returning a value.

Please log in or register to report this question.

88.

What is the output of this C code?

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

 

Answer: D

Correct answer: 0

ptr holds the address of x, so *ptr retrieves what is stored there, which is 0. There is nothing indeterminate about it - x was explicitly initialised, and the pointer was set from its address, so the dereference is entirely defined. Printing the address rather than the value would require %p and the pointer itself.

Please log in or register to report this question.

89.

What is the output of this C code?

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 is a value, not an object, so it cannot be incremented: the ++ operator needs a modifiable lvalue and the result of & is neither modifiable nor an lvalue. The next question shows the working version, in which a pointer variable holds the address and it is the variable that is incremented.

Please log in or register to report this question.

90.

What is the output of this C code?

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

The postfix ++ yields the value the pointer had before the increment, so foo receives the original address of i and prints 10. The increment happens to p in main, which afterwards points one int past i - harmless here since it is never dereferenced. Prefix ++p would have passed the advanced pointer and the behaviour would then have been undefined.

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