Multiple choice technology

Which of the following is the correct way of declaring a float pointer:

  1. float ptr;

  2. float *ptr;

  3. *float ptr;

  4. None of the above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In C, a pointer to float is declared using an asterisk (*) before the variable name: float *ptr;. Option A (float ptr;) declares a regular float variable, not a pointer. Option C (*float ptr;) has incorrect syntax - the asterisk must come after the type, not before it.

AI explanation

To answer this question, you need to understand how to declare a pointer in C or C++.

A pointer is a variable that stores the memory address of another variable. When declaring a pointer, you need to specify the data type of the variable it will be pointing to. In this case, we want to declare a pointer to a float.

Option A) float ptr; - This option is incorrect because it declares a regular float variable, not a pointer.

Option B) float ptr; - This option is correct because it declares a pointer to a float. The "" before the variable name indicates that it is a pointer.

Option C) *float ptr; - This option is incorrect because it tries to declare a variable named "ptr" of type "*float", which is not a valid data type.

Option D) None of the above - This option is incorrect because option B is the correct way to declare a float pointer.

The correct answer is B) float *ptr;