Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does (node *) NULL mean in C?

I was going through a book studying Linked list and saw these lines

if( *head == NULL){

}else if ( (*head)->next == (node *) NULL ){

}

what is the difference between NULL and (node *) NULL can they be used interchangeably?

typedef struct nodeType{
    int info;
    struct nodeType *next;
}node;
like image 413
Nikhil Bhandari Avatar asked Mar 22 '12 03:03

Nikhil Bhandari


People also ask

What does node * head null mean?

Whenever you are creating a linked list, we have to initialize it and since it doesn't have any nodes in the start (empty linked list in the beginning), the start/head points to null. That's what is meant by *head=NULL.

What does it mean when a node is null?

The null reference occurs in the link part of the final node of a linked list. A program that maintains a head and a tail reference may set these references to null, which indicates that the list is empty (has no nodes).

What happens when you set a node to null?

current = null merely reassigns a value of this variable. It does not change the underlying Node object.

What is meaning of -> next -> Next null?

next != null means when the loop exits, tmp. next will be null , which is only true for the last node.


2 Answers

When comparing pointers, types are not considered, so it is pointless.

The author likely just included it for clarity if it's an introductory book. If it's not an introductory book, then the author either has an odd coding style, or somehow thinks that it's more meaningful.

like image 187
Corbin Avatar answered Oct 18 '22 18:10

Corbin


They can be used interchangeably. But it is non-standard and unusual to typecast NULL as your code does.

No cast is required.

like image 26
Jonathan Wood Avatar answered Oct 18 '22 19:10

Jonathan Wood