I am trying to print some integer data which includes the integer 0, but I want to ignore the data containing NULL and not print it.
but my C code can't distinguish between 0 and NULL and treats them as if they are the same.
void ecrire(struct node *p) {
if (p->data == NULL) { 'if the first node contains NULL it means list is empty'
printf("no items!\n");
return;
}
struct node *temp = p;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
when I pass a list containing the value 0 in the first node it treats it as if it's an empty list. any solution Please?
data? Unless it isint, the statementprintf("%d ",temp->data)yields undefined behavior, since%dexpects this type.NULLmay be defined as zero. For historical purposes, the C standard allows it to be zero (an integer constant expression with value zero) or zero cast tovoid *. Good C implementations ought to define it as((void *) 0, not as0.dataandnextfields, before worrying about nuances of C null pointers.