Background
I was writing code that uses functions from ctype.h
to identify things in strings. I accidentally passed the string (char*
) to the function(s) which take and int
type, causing the program to segfault. It was easy enough to see that I forgot to dereference the string pointer, but GCC gave me no warnings even when compiling with the following arguments:
gcc -o main main.c -Wall -Wextra -Werror -pedantic -pedantic-errors -std=c99 -Wconversion
Debian GNU/Linux bookworm 12.5 x86_64
和gcc (Debian 12.2.0-14) 12.2.0
都是最新的.下面是一个问题的例子:
/* main.c */
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char msg[] = "hello";
int res = isspace(msg); // char* gets cast to int without warning
// It should be `isspace(*msg)`
// This also segfaults
printf("%i\n", res);
return 0;
}
Questions
- 我可以打开什么警告来获得这些指针到整数转换的编译时错误?
- 为什么这甚至是一开始的分割错误?