我有以下代码:
我有以下代码:
#include <inttypes.h>
#include <stdlib.h>
struct a
{
void *p;
};
int main(void)
{
struct a *ptr = malloc(sizeof(struct a));
ptr->p = malloc(sizeof(uint8_t));
*((uint8_t *) ptr->p) = 2;
return 0;
}
我在解除引用之前将空指针转换为void指针以避免警告。
警告:解除引用'void *'指针
这样做是否违反了任何规则,或者这段代码是否正确?
p
的类型不是uint8_t*
,那么这段代码就很糟糕。 - Stavenvoid *p
,并且不能干扰该结构体但想要使用它。这不是糟糕的代码。 - Gopi#include <stdlib.h>
。 - M.M