这个语句在 C 中是什么意思:“(void)ptr;”

What does this statement mean in C: "(void)ptr;"

从标题上看可能不是很清楚。我在嵌入式 STM32 项目中遇到了以下代码。我不明白函数里面的那一行。

    static void txend1(UARTDriver *uartp) {
        (void)uartp; // what does this do? Is it a statement?
    }

我试过在网上其他地方搜索,但大多数结果都是将指针转换为 void 指针,我认为这不是。感谢您的帮助!

这只是这个未使用的 uart 参数的 portable way to suppress the warning

它没有任何作用,但编译器认为它已被使用,并且不会发出任何警告。

当函数的原型被强加/无法更改(回调函数)但您的实现不需要此参数时非常有用。

(注意 gcc 支持 __attribute__((unused)) 结构,更容易理解,但不兼容所有编译器)