2

I've seen people declaring structs like this in C:

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

Why do we have *head after the curly bracket? How would head be used later in the program?

Also, why is it valid to use a struct inside itself? I think it should be defined first before being used.

2
  • that is bad programming practice. *head is a variable,pointer of type struct node Commented Feb 20, 2015 at 1:24
  • 1
    The relevant section of the C99 standard was §6.7.2.1/2: A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), ... So the standard says that a structure may contain a pointer to its own type, and sufficient is known about struct node once the { is reached for the embedded reference to be OK. (I just happened to be looking at an answer of mine where that section was quoted, mainly for the material in the '...' which I've left out here.) Commented Feb 20, 2015 at 1:48

4 Answers 4

3

Why do we have *head after the curly bracket?

The code:

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

is declaring a variable called head, which is a pointer to an instance of struct node. It is not good practice to mix the definition of a type and the definition of variables of that type in a single statement and you should generally avoid doing so.

It is equivalent to:

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

struct node *head;

why is it valid to use a struct inside itself?

Inside the struct, there is a struct node* declared. This is simply a pointer to another node structure instance. This is quite commonly used when implementing data structures in C such as linked lists.

Sign up to request clarification or add additional context in comments.

Comments

1

Why do we have *head after the curly bracket? How would head be used later in the program?

  • Because it's bad style and it's declaring a global variable of type struct node * named head.

Also, why is it valid to use a struct inside itself? I think it should be defined first before being used.

  • You can always declare pointers to incomplete types, and then at compile time when dereferencing the pointer, the definition must be available, this wouldn't work for example

This is from the c standard draft 1570, §6.7.2.1

  1. A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.

I've highlighted the relevant parts. So it means that it can be a pointer, but not an instance of itself, an example of the meaning of the qoute

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

would be invalid because the type struct node is not defined, but this would do it

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

and it's equivalent to the original code.

This feature is what makes the wonderful linked lists possible, because since next is a pointer to a struct node then you can link current node, to another node of the same type and so on, it's normally done such that last node, is linked to a special value, NULL, this would be a very simple example

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

int main(int argc, char **argv)
{
    struct node *head;
    struct node *next;
    struct node *current;

    head = malloc(sizeof(*head));
    if (head != NULL)
    {
        head->data = 1;
        head->next = NULL;
    }

    next = malloc(sizeof(*next));
    if (next != NULL)
    {
        next->data = 2;
        head->next = next;
        next->next = NULL;
    }

    for (current = head ; current != NULL ; current = current->next)
        fprintf(stderr, "node@%p: %d\n", current, current->data);

    free(next);
    free(head);
    return 0;
}

a more sophisticated program would have a function to create/append/prepend nodes from the list.

1 Comment

You might want to copy the quote from the standard in my comment to the main question into your answer to support your example of what is and is not allowed.
0

Also, why is it valid to use a struct inside itself

  • pay attention, that's not a struct itself, it's a pointer to it and that is valid.

Comments

0

The *head declaration after the curly bracket creates a pointer type to struct node. You can declare a pointer to the structure without having to write struct node *

Although it is not a good practice and it should be avoided.

In addition, this is optional.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.