如何以正确的方式转换void **指针?

问题描述 投票:-1回答:1

鉴于

void func(uint8_t type, void **structs)
{
    switch(type)
    {
        case 1:
            a1 = (UI_Remote_s*)structs->Method;         //expression must have pointer to struct
            a2 = ((UI_Remote_s*)structs)->Method;       //correct
        break;
    }
}

第一次尝试是错误的。为什么?

c casting operator-precedence
1个回答
2
投票

我不会使用这种类型的转换,因为它使代码更难阅读。而是使用临时变量来存储指针。它将使代码更易于阅读和理解。编译器很可能在生成的代码中对其进行优化。

UI_Remote_s **ptr = (UI_Remote_s **)structs;
a2 = (*ptr) -> Method;
a2 = (*(ptr + 5)) -> Method;
a2 = ptr[2] -> Method;
.....
© www.soinside.com 2019 - 2024. All rights reserved.