If we try something like
char *str = "hello";
if (isalpha(str))
printf("%s\n", str);
we'd get a segmentation fault, because isalpha()
takes int
whose value must be in the range of an unsigned char
or EOF
.
But why doesn't the compiler complain about passing a char *
where an int
is expected? If char *
gets implicitly-casted down to int
, we might lose data!
In fact, if we have something like
void doSomething(int i);
int main(void)
{
char *str = "hello";
doSomething(str);
}
void doSomething(int i)
{
// some code
}
The compiler will give this warning:
warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
Assuming -Wpointer-to-int-cast
is enabled by default, what's the difference here?