M
matevzb
I've ran into some fishy code that, at first glance, is buggy, but it
seems to work correctly
and none of the compilers I've tried (five so far, on various systems)
gives any warnings.
The code:
============================
#include <stdio.h>
void
fcn (char *str)
{
if (str == '\0')
{
printf ("str!\n");
}
}
int
main (void)
{
fcn ('\0');
return 0;
}
============================
My understanding so far: '\0' is passed to fcn(), it equals integer 0,
which in turn "equals"
a NULL pointer in terms of zero-ness. The 0 in fcn() is then compared
to '\0' which is also a 0.
No warnings, no errors, code works (or so it seems).
But - if I pass e.g. '\1' to fcn() than all the compilers complain
with similar warnings,
i.e. making a pointer from integer without a cast.
My question is, why don't the compilers complain when '\0' is passed?
Shouldn't they give
the same warning, as it's also an integer (albeit 0 in value)?
seems to work correctly
and none of the compilers I've tried (five so far, on various systems)
gives any warnings.
The code:
============================
#include <stdio.h>
void
fcn (char *str)
{
if (str == '\0')
{
printf ("str!\n");
}
}
int
main (void)
{
fcn ('\0');
return 0;
}
============================
My understanding so far: '\0' is passed to fcn(), it equals integer 0,
which in turn "equals"
a NULL pointer in terms of zero-ness. The 0 in fcn() is then compared
to '\0' which is also a 0.
No warnings, no errors, code works (or so it seems).
But - if I pass e.g. '\1' to fcn() than all the compilers complain
with similar warnings,
i.e. making a pointer from integer without a cast.
My question is, why don't the compilers complain when '\0' is passed?
Shouldn't they give
the same warning, as it's also an integer (albeit 0 in value)?