Consider the following C++ code:
void* a = &a;
Why doesn't the compiler complain for using an undeclared identifier?
Also, what does the compiler consider the variable a
to be? Is it a pointer to a void object or is it a pointer to a void*
pointer?
void (C++)When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters.
void* is a pointer type that doesn't specify what it points to.
yes it is safe.
Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.
The scope of declaration of variables in C++ can be pretty surprising:
void* a = &a; ^~~~~~~~~~~~~~~~~ a declared as `void*` from here on
Therefore, &a
is void**
but since any pointer type is implicitly convertible to void*
...
It is equivalent to
void* a; a = &a;
Therefore, a
has been declared. So a
gets the address of a
written in a
. So it is a pointer to a void pointer. (You did not define any objects yet.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With