Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


Why must you cast a void pointer before dereferencing it?

  1. To convert it to a null pointer

  2. To specify the type of data it points to

  3. To decrease the pointer's memory usage

  4. C++ does not allow void pointers to be dereferenced

The correct answer is: To specify the type of data it points to

A void pointer is a generic pointer that does not contain information about the type of data it points to. Therefore, before dereferencing it, we must cast it to specify the type of data it points to. This allows the compiler to correctly interpret the data and perform the dereferencing operation. Option A is incorrect because casting to a null pointer would not provide the necessary type information for dereferencing. Option C is incorrect because casting a pointer does not change its memory usage. Option D is incorrect because C++ does allow void pointers to be dereferenced as long as they are first cast to the correct type.