Which of the following is the correct way of declaring a float pointer:
-
float ptr;
-
float *ptr;
-
*float ptr;
-
None of the above
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.
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;