c++ - Pointer declaration with and without struct? -


this question has answer here:

what's difference between:

  struct name{     int example;     name *next;     }; 

struct name *next= null;

...and

name *next=null;` 

(defined after data structure, when linked list still empty) ?

first of data member name next in structure

struct name {     int example;     name *next; }; 

and variable same name declared after structure example

struct name *next = null; 

are 2 different entities.

the last declaration not initialize null data member of object of structure. declares pointer object of type of structure.

now difference between 2 declarations

struct name *next = null; 

and

name *next = null; 

in first 1 there used so-called elaborated type name struct name. advantage compared second declaration object, enumerator or function declared same name name hide declaration of structure. example

struct name {     int example;     name *next; };  enum { name, noname }; 

here enumerator name hides data type struct name , if write example

name *next = null; 

then compiler issue error.

but if use elaborated name

struct name *next = null; 

then code compiles because compiler knows name in declaration struct name.

another important difference.

consider following code snippet

int main() {     struct name     {         int example;         name *next;     };      {         name *next = null;     } } 

in progam declaration within inner code block declares pointer of type of struture declared in outer code block.

now rewrite program

int main() {     struct name     {         int example;         name *next;     };      {         struct name *next = null;     } } 

in case declaration in inner code block introduces new type struct name hides structure declaration in outer code block.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -