J
jaysome
struct Struct *astruct = NULL;
suddenly this becomes a valid parameter to strcmp comparing equal to ""
It does not happen so suddenly. If you have some contrived code like
the following:
#include <string.h>
int main(void)
{
struct Struct *astruct = NULL;
(void)strcmp(astruct,"");
return 0;
}
then it should not even compile, under Standard C.
The Standard C function strcmp() has a prototype whose first argument
is of type const char *. The above code passes an object of type
struct Struct * as its first argument to strcmp(). The types const
char * and struct Struct * are not compatible (meaning that there is
no implicit conversion between the two), and a Standard C-compliant
compiler MUST reject such an incompatibility.
Some might think that a simple "fix" is to explicitly convert astruct
to type const char * or just char *:
#include <string.h>
int main(void)
{
struct Struct *astruct = NULL;
(void)strcmp((char *)astruct,"");
return 0;
}
Never, ever, do this type of thing under these circumstances.
Silencing compiler warnings or errors with explicit casts can get you
into real, terrible, trouble.
Best regards