I have a Code example here.
struct node {
int data;
struct node *link;
};
static struct node *first = NULL;
It would be great if someone could throw some light on my below questions about the usage of the word static.
What does the keyword static do in the above code?
what is the difference between normal structure and static structure?
It creates a static pointer to a node
and initializez it to NULL
.
The variable definition can have multiple meanings:
static struct node *first = NULL;
If defined outside of a method, it gives first
internal linkage. It can only be used inside the defining module.
But you can also find that line inside a method:
void foo()
{
static struct node *first = NULL;
}
The variable is a method-scoped variable residing in static storage. It is initialized to NULL
once and all subsequent changes persist between calls to the function.
It means that this variable may not be used outside this module.
E.g. - you cannot reference this pointer from another file using
extern struct node *first;
An important note is that the struct is not static, only first
which is a pointer to such structure is static.
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