struct node
{
int data;
struct node *next;
}*head;
Why do we have *head? And is this different (better?) than doing
typedef struct
{
int data;
struct node *next;
}head;
struct node
{
int data;
struct node *next;
}*head;
Why do we have *head? And is this different (better?) than doing
typedef struct
{
int data;
struct node *next;
}head;
The first section defines a variable head
which is a pointer to a struct node
type. It can be NULL, indicating that your linked list has zero elements.
The second block just declares a type called head
. It's not a variable at all. And it does not compile as the type of its next
field, struct node
, does not exist.
You probably wanted
typedef struct node {
int data;
struct node *next;
} node;
node *head;
This form declares 2 types, struct node
and node
(the same), and defines a variable head
. I'd go for the 1st form without the typedef, as it's more simple, and you cannot refer to the typedef inside the struct's next
field anyway.
The first version declares a type (struct node
), and a variable head
which is a pointer to struct node
. All lists need a head, so that's helpful.
The second declares head
as the type name for an (otherwise unnamed) struct
. It won't actually compile, since the inner struct node *next
refers to a type which doesn't exist.
In your first example, you define the struct
but also declare the head
variable to be a pointer to such struct
.
In your second example (typedef
) you just declare some type synonym (but
no variables!)
struct node
{
int data;
struct node *next;
}*head;
in the code above you have declare object head as a pointer so in order to access its element data you need to use -> operator that is for example head->data=4; and so on
you can create nodes to append the head pointer
typedef struct
{
int data;
struct node *next;
}head;
where as in this code every node that you create will be of head type it will create a problem when you delete the head node if you write linked list code for deleting head you are unable redefine the new head
where as in earlier code head being a pointer can be change by updating its next element in struct after updating the head->next in previous code we can then easily delete the previous head without losing the new head
the gdb debugger (and perhaps other debuggers)
can only see the contents/format of a struct that contains a tag name.
The use of a typedef hides the tag name and defines a new type.
however, gdb will not properly display the contents of any instance of the struct.
The correct way is:
struct tagname { ... };
Notice no struct name to clutter the symbol table and create confusion
Of course, then each defining instance of this struct needs to be written as:
struct tagname myName;
but that should not be a problem and adds the visible documentation
that myName is a struct
Along with the increased visibility of the struct, both to gdb and the reader
of the resulting code, any pointer to that struct should never be defined
as part of the struct definition. Such a formatting hides the
fact that such name is a pointer. rather declare an instance of a pointer as:
struct tagname * pMyStruct;
And, never use this syntax:
struct { ... } structName;
as that is a depreciated syntax