Please see the following code:
char h[256];
h[0]=NULL;
if(h!=NULL)
{
printf("It doesn't show NULL\n");
}
else
{
printf("It shows NULL\n");
}
and also the following:
char h[256];
if(h!=NULL)
{
printf("It doesn't show NULL\n");
}
else
{
printf("It shows NULL\n");
}
and also the following:
char h[256];
h[0]='\0';
if(h!=NULL)
{
printf("It doesn't show NULL\n");
}
else
{
printf("It shows NULL\n");
}
In every case the char* h doesn’t have NULL. Why is it the case? Isn’t it suppose to have NULL as I’m not storing anything there? And if it’s not the case, how I can I make sure that it contains nothing but NULL?
h[0]is not the same ash.h[0]is the first character in the array;his the array itself (which decays to a pointer in this situation).Try this:
Note also that you probably shouldn’t be using
NULLin this situation.NULLis for pointers; you want'\0'instead.