Farooq Khan
Farooq Khan

Reputation: 305

What does the following C++ line of code mean?

The following block of code is taken from a C++ program which implements a queue. I know struct, classes and pointer etc. but I dont use struct a lot so it is giving me a hard time understanding what *front = NULL,*rear = NULL,*p = NULL,*np = NULL; means. Are these node type pointers being declared and are being set to a default value of NULL? Please correct me if I'm wrong and kindly explain.

struct node
{
    int data;
    node *next;
}*front = NULL,*rear = NULL,*p = NULL,*np = NULL;

Upvotes: 0

Views: 110

Answers (2)

Frank Puffer
Frank Puffer

Reputation: 8215

It means the same as this:

struct node
{
    int data;
    node *next;
};

node *front = NULL;
node *rear = NULL;
node *p = NULL;
node *np = NULL;

And it is definitely no good style.

By the way, this would also work if node was a class. A struct is basically a class with all elements being public by default.

Another recommendation: C++11 has a specific keyword nullptr for initializing pointers. This expresses more clearly what is happening. NULL is just a preprocessor macro that expands to 0.

Upvotes: 2

Lassie
Lassie

Reputation: 858

It's the same as

struct node
{
    int data;
    node *next;
};
node *front = NULL, *rear = NULL, *p = NULL, *np = NULL;

Upvotes: 3

Related Questions