如何让GCC编译器报告float指针转整数指针的强制转换警告?
Great question! Your current compile flags (-pedantic -Wall -Wextra -Wconversion) cover a wide range of issues, but they don’t explicitly warn about casting pointers between incompatible arithmetic types like float32_t* to uint32_t*.
To get GCC to trigger this specific warning, you need to add the -Wcast-pointer-type flag. Introduced in GCC 10, this flag detects explicit casts between pointer types that are not compatible—exactly the scenario your static analyzer flagged.
Updated Compile Command
Your revised set of flags will look like this:
-pedantic -Wall -Wextra -Wconversion -Wcast-pointer-type
What the Warning Looks Like
When you compile your code with this flag, GCC will output a warning similar to:
warning: cast between incompatible pointer types [-Wcast-pointer-type]
foo( (uint32_t*)x );
^~~~~~~~~~~~~
Bonus: Catching Undefined Behavior
If you also want to flag the potential undefined behavior from accessing a float32_t through a uint32_t* pointer, enable strict aliasing checks with -Wstrict-aliasing=2 (or -Wstrict-aliasing, which defaults to level 2). This will warn about invalid type aliasing, a common risk with such casts.
Content of the question originates from Stack Exchange, asked by Danijel

