gpt4 book ai didi

c - 如何将 volatile 变量传递给 c 中的函数?

转载 作者:太空宇宙 更新时间:2023-11-04 05:24:36 26 4
gpt4 key购买 nike

我想将以下内容传递给 c 中的函数:

#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))

以下似乎工作得很好:

void ZeroRegister(unsigned long * _REG) 
{
#define vReg (*((volatile unsigned long *)_REG))
vReg = 0;
}

有没有更好的编码方式?还是我的代码有缺陷?除了它生成的编译器警告。

最佳答案

您编写的函数需要一个指向 volatile 内存的指针,这样您就可以避免警告(并且函数中不需要额外的宏):

#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))

static void ZeroRegister(volatile unsigned long *REG)
{
*REG = 0;
}

int main(void)
{
ZeroRegister(&GPIO_PORTF_DATA_R);
return 0;
}

在带有 GCC 5.3.0 的 Mac OS X 10.11.4 上,使用显示的命令行进行干净编译(源文件是 vui.c):

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -Werror -c vui.c
$

从函数参数中省略 volatile 会导致警告(行号和变量名与上面的代码不匹配;它们是从早期版本的代码生成的):


vui.c: In function ‘main’:
vui.c:11:18: error: passing argument 1 of ‘ZeroRegister’ discards ‘volatile’ qualifier from pointer target type [-Werror=discarded-qualifiers]
ZeroRegister(&GPIO_PORTF_DATA_R);
^
vui.c:3:6: note: expected ‘long unsigned int *’ but argument is of type ‘volatile long unsigned int *’
void ZeroRegister(unsigned long * _REG)

请注意,上面的编译代码避免使用为实现保留的名称。所有以下划线和大写字母(或另一个下划线)开头的名称都保留用于实现。请参见 ISO/IEC 9899:2011 §7.1.3 保留标识符:

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

关于c - 如何将 volatile 变量传递给 c 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36540757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com