C 中带有 #define 的指针
函数:
#define ASSOC(port) (*(volatile bit_field *) (&port))
函数调用:
#define SCLK ASSOC(PORTC).bit0
bit_field 定义为这样的结构:
typedef struct {
unsigned char bit0 :1, bit1 :1, bit2 :1, bit3 :1, bit4 :1, bit5 :1,
bit6 :1, bit7 :1;
} bit_field;
我不知道 &port 是在哪里定义的。
有人可以解释一下该函数是如何读取的以及它是如何工作的吗?我不太擅长指针,尤其是这个例子,前面和末尾的“*”以及“&”非常令人困惑。与端口。
谢谢
The function:
#define ASSOC(port) (*(volatile bit_field *) (&port))
The function call:
#define SCLK ASSOC(PORTC).bit0
bit_field defined as a struct like this:
typedef struct {
unsigned char bit0 :1, bit1 :1, bit2 :1, bit3 :1, bit4 :1, bit5 :1,
bit6 :1, bit7 :1;
} bit_field;
I don't know where &port is defined.
Can someone please explain how the function is read and how it works please? I am not very good with pointers and this example in particular is very confusing with "*" in the front and at the end and the "&" with the port.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
端口
未定义。这是 ASSOC 的参数,您可以在此处看到:此外,我认为它应该是一个
char
,因为bit_field
有 8 位。&port
中的&
只是用于获取其在内存中的地址。ASSOC 依次将
&port
转换为易失性 bit_field *
,然后在开头有额外的*
直接指向内容结果指针。因此,一旦调用 ASSOC(port),就可以将其用作 bit_field 类型结构体。例如,
SCLK
宏将给出PORTC
的第一位。port
is not defined. It is the argument to ASSOC as you can see here:Also, I assume it is supposed to be a
char
, because thebit_field
has 8 bits. The&
in&port
simply serves to get its address in memory.ASSOC in turn casts
&port
to avolatile bit_field *
and then there is the extra*
at the beginning to point directly to the contents of the resulting pointer.Therefore, once you call ASSOC(port), you can use it as a bit_field type structure. For example, the
SCLK
macro will give the first bit ofPORTC
.您定义的 ASSOC 宏是就地转换。
port
(在本例中,它来自另一个宏SCLK
)ASSOC
获取port<的地址/code> 与
&
运算符ASSOC
将port
的地址转换为(易失性位域 *)
*
s) 地址bit_field
的结果与您开始时的位相同,但可用作
bit_field
结构。The
ASSOC
macro you define is an in-place cast.port
(in this case, it's coming from the other macroSCLK
)ASSOC
takes the address ofport
with the&
operatorASSOC
castsport
's address to a(volatile bit_field *)
ASSOC
dereferences (*
s) the address of thebit_field
The result is the same bits as you started with, but usable as a
bit_field
struct.ASSOC
采用我假设的名为PORTC
的char
并返回一个具有相同值的bit_field
PORTC
中的每个单独位。SCLK
返回位字段中的一位。您将使用
PORTC
并将其作为参数传递给ASSOC
。ASSOC
takes what I'm assuming is achar
namedPORTC
and gives back abit_field
with the same values as each individual bit inPORTC
.SCLK
returns one of the bits in the bit field.You're taking
PORTC
and passing it toASSOC
as the parameter.