I'm writing some list library as an exercise in C. I wonder whether C specification allows that the declaration of a variable and the reference to it at the same time, like:-
typedef struct _List {
int value;
struct _List* next;
} List;
List* TERMINATOR = {0, &TERMINATOR};
In short, is it possible to write void* p = &p; ?
It seems gcc allows this, but I want to know how the specification is.
Thank you in advance.
void *p = &p;
is valid. Variables are in scope in their own initialization.
However,
List* TERMINATOR = {0, &TERMINATOR};
is not valid because TERMINATOR is a pointer here, not a struct.
If you remove the *,
List TERMINATOR = {0, &TERMINATOR};
is valid.
Reference: ISO 9899:1999, 6.2.1 Scopes of identifiers:
- Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.
(Emphasis mine).
In void *p = &p, the declarator is *p.
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