c - 类型转换指针取消引用

标签 c pointers casting dereference

在整理退休工程师的代码时,我遇到了一个相当简单的宏,但我的 C 知识不太好。

#define mem32(addr) (*(unsigned long volatile *)(addr)) 
  1. 我是否正确地将其称为类型转换指针取消引用?
  2. 它类型转换addr然后取消引用它?或者反过来呢?这重要吗?
  3. 顺序对于类型限定符和类型说明符重要吗?我假设(在此之前)类型限定符必须位于类型说明符之前,但事实并非如此
  4. 语法问题。第二个 * 的用途是什么?

最佳答案

  1. Am I correctly calling this a type casting pointer dereference?

是的。

  1. It types casts addr and then derefences it? Or the other way around? Does it matter?

*(unsigned long volatile *)(addr) 正在对 addr 进行类型转换,然后取消引用它。

  1. Does order matter for type qualifiers and type specifiers?

没有。顺序并不重要。 C11 第 §6.7.2/2 节

[...] the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers. [...]

关于c - 类型转换指针取消引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34774888/

相关文章:

c++ - 返回指向接口(interface)的指针,但将所有权和生命周期保留给提供者

c - ANSI C : Declare a pointer of type "array of string"

我可以在 C 中将 0.16、0.32 等正确转换为整数(16、32 等)吗?

iphone - 我必须创建一个新对象还是可以使用方法参数?

C++ - 函数指针和类

c - printf() 和 float 错误

c - 如何读取文件直到C中的一个字符

c - 如何读取c中特定列的行?

使用不接受任何输入事件或获取焦点的 xlib 创建透明窗口

c - 尝试以持续时间和频率发出蜂鸣声时出现 ioctl 错误