Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between static structure and normal structure?

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.

  1. What does the keyword static do in the above code?

  2. what is the difference between normal structure and static structure?

like image 403
karthik gorijavolu Avatar asked Mar 06 '12 09:03

karthik gorijavolu


2 Answers

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.

like image 64
Luchian Grigore Avatar answered Oct 04 '22 03:10

Luchian Grigore


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.

like image 34
MByD Avatar answered Oct 04 '22 02:10

MByD