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

Discussion

Which of the following does not initialize ptr to null (assuming variable declaration of a as int a=0)?

  • A.int *ptr = &a;
  • B.int *ptr = &a - &a;
  • C.int *ptr = a - a;
  • D.All of the mentioned

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.