I have a linked list structure
struct node {
int data;
node next;
}
and a function
struct node *insert_sorted (struct node *nodes,
int d) {
// todo
}
I need to implement this so that, given a new piece of data, it constructs a new node and inserts into the linked list in sorted order.
The first thing I want to check inside of the insert_sorted
function is whether nodes
points to nothing or something. By that I don’t mean checking whether the pointer is NULL
, which I’m familiar with. I mean that I want to check whether the address nodes
holds a node struct or not. I think if this were Java I might be able to write
if (nodes == null) {
...
}
but I don’t think there is an analogous thing to do in C.
I know that I could try to manage the nodes
pointer so that, if it is non-null then it always points to some struct. I’ll probably end up doing this.
But I just want to know if there exists a more direct way of solving the issue. Is there any way to take a given non-null pointer and check whether it points to anything or not?