PsychedGuy
PsychedGuy

Reputation: 187

What is the differences in these declarations?

I am learning about uses of data structures and came across some doubts:

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

Question1: In the above structure, what does declaring a struct node *next within the structure mean?. And how is it different from simply declaring a pointer variable within structure as int *next

Question2: We can see that, at the end of the structure definition, a pointer *head is declared of type node and is used to access the structure members if I am right. Is this same as declaring it like this:

                            struct node *head;

Any help with this would be great. Thank you all in advance.

Upvotes: 1

Views: 68

Answers (3)

Purag
Purag

Reputation: 17061

This is very commonly how Linked Lists are declared in C. Recall — a linked list is a series of nodes which contain data as well as a link (in this case, a pointer) to the next node in the series.

So, each object of type struct node should contain a pointer to another struct node. This is characterized by declaring a field next in the struct with type struct node *.

As for your second question: the code you provided both defines struct node (allowing you to instantiate objects of type struct node), and declares a pointer to such a struct called head. You could certainly define the variable head the way you asked, but you would still need to define the struct somewhere earlier in the code. This is usually done in a separate header file, but could be done in the same .c source code file for brevity.

Now, note that both these methods will not actually create a struct node object. All you're doing is declaring a pointer to one. The object doesn't yet exist anywhere since you never allocated space for it. To do that, you'd need to use malloc:

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

head = malloc(sizeof(struct node));

You could also declare a runtime stack instance of the struct, which just means you won't need to explicitly free the memory you allocated with malloc(). The memory allocated for the struct is deallocated when you return from the function, or when your program terminates if declared outside of any functions.

Upvotes: 0

Tom Karzes
Tom Karzes

Reputation: 24052

Declaring next as struct node *next means that it points to a struct node, presumably the next node in the list. If it were instead declared as int *next then it would point to an int, which it doesn't seem there's any need for.

The answer to question 2 is yes. They are simply combining the definition of struct node with the declaration of head. They could be split apart as you indicated.

Upvotes: 0

theharls
theharls

Reputation: 163

"struct node *next" declares a variable called next which points to a "struct node". In other words, a singly linked list.

You are correct in that the statement does two things:

  1. Declares a struct called node (with an int called data and a pointer to the next struct node)
  2. Declares a variable called head pointing to the struct node.

These could have been done separately as follows:

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

struct node *head;

Upvotes: 1

Related Questions