我有以下代码:
typedef struct S1 S1_t;
struct S1
{
uint8_t *ptr1;
uint8_t **ptr2;
};
void get_ptr1(const S1_t *s1, uint8_t const **ptr1)
{
*ptr1= s1->ptr1;
}
void get_ptr2(const S1_t *s1, uint8_t const *const **ptr2)
{
*ptr2 = s1->ptr2;
}
如果我让它保持原样,我会收到GCC编译器对函数get_ptr2
发出的以下警告:
assignment to ‘const uint8_t * const*’ {aka ‘const unsigned char * const*’} from incompatible pointer type ‘uint8_t **’ {aka ‘unsigned char **’}
为了使警告静音,在将其赋给ptr2
之前,我必须将其强制转换为s1->ptr2
:
void get_ptr2(const S1_t *s1, uint8_t const *const **ptr2)
{
*ptr2 = (const uint8_t *const *)s1->ptr2;
return 0;
}
我不明白为什么我需要在这里做这个转换操作,当我不需要在get_ptr1
中做(const uint8_t *)
转换时?
我在这里的目标是实现 struct S1
‘S成员的Getter函数,我想确保获取这些成员的函数不能修改它们.因为我在这里做了一些危险的事情,所以编译器会向我发出警告吗?
我有-wall
和-wextra
编译选项标志启用.