c - C 中 (void)variable 的用途是什么?

标签 c embedded

<分区>

我正在阅读 this book 并且遇到了这段代码:

static void
task1(void *args) {
    int i;

    (void)args;

    for (;;) {
        gpio_toggle(GPIOC,GPIO13);
        for (i = 0; i < 1000000; i++)
            __asm__("nop");
    }
}

除了第 5 行,我都(相对)理解了。(void)args; 在做什么?

args 不在函数体中使用,我知道如果不使用参数则可以这样写

static void
task2(void *args __attribute((unused))) {
     // code here
}

这里没有做。那么这样写的(void)是做什么的呢?

最佳答案

一般来说,当一个人不使用函数参数时,编译器可能会警告你。毕竟,如果您不打算使用它,为什么首先要将它放在那里?

实际上,它是对参数的人为使用,除了告诉编译器您知道自己在做什么之外没有任何用处,因此“Shatupalready with the warning!”

(void) args 适用于我见过的所有平台。我相信 __attribute((unused)) 是 gcc 特定的东西。

关于c - C 中 (void)variable 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58019275/

相关文章:

.net - 嵌入式系统的数据库

c - 减法中 16 位 int 机器和 32 位 int 机器之间的不同结果

c - Understanding ShellSort code from c K&R Book at page 62

c++ - Keil5 C工程转C++工程报错用中断函数

c - STM32L072KBU 从应用程序代码跳转到引导加载程序(系统内存)

arrays - 从不兼容的指针类型传递 'functionName'的参数1

linux - 保护 RPi SD 卡在日常电源循环中免遭损坏

c - 如何获取存储在双引号内的值

c - C 编程中带有 "struct"的 "for-loop"对象

c - strcpy 在 c 中给出段错误