I have the below code:
#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;
}
I am casting the void pointer before dereferencing to avoid the warning
warning: dereferencing ‘void *’ pointer
Am I breaking any rule by doing this or is this code good?