gpt4 book ai didi

c - C 中的符号 (((nodeptr) (bodyptr)) -> type) 是什么意思?

转载 作者:行者123 更新时间:2023-11-30 18:51:55 24 4
gpt4 key购买 nike

我遇到了一段如下所示的代码,其中nodeptr和bodyptr是指向结构的指针,而type是结构的成员。

#define Type(x) (((nodeptr) (x))->type)

括号中两个相邻的指针是什么意思?我知道 -> 符号获取结构的成员,但不确定该行的第一部分。我对 C 相当陌生,正在努力了解指针!

最佳答案

这是一个 Actor 阵容。
在这部分中,((nodeptr)(bodyptr)),指针bodyptr被强制转换为nodeptr类型的指针,然后它访问成员bodyptr指向的结构体的type

void *GetStructPtr(void);    //The function returns a pointer to void

typedef struct //This is our structure
{
float a;
int type;
} node;
type def node *nodeptr; //This is our pointer type

void my func(void)
{
void *bodyptr; //Here we have a generic void pointer
bodyptr = GetStructPtr(); //Assign to it the vallue returned from function
//In the next line we cast our void* to a pointer to a structure node
//and then access its member type.
((nodeptr)bodyptr)->type = 0;
}

就您而言,它已被插入到宏中以使其更易于使用。

关于c - C 中的符号 (((nodeptr) (bodyptr)) -> type) 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35576136/

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