logoalt Hacker News

amiga386today at 12:53 PM1 replyview on HN

Tell me why struct* is incompatible with void* when it's such a standard case in C that you don't need a cast:

    struct foo *x = malloc(sizeof(struct foo)); /* malloc returns void* */
Or rather, tell me why the C11 standards committee decided to declare that struct* is incompatible with a void*

Replies

tomptoday at 1:05 PM

ok so Claude says I was wrong, it's more subtle.

(1) you can cast between any pointer types (no UB - assuming they're aligned), but accessing memory through a wrongly-typed pointer is UB

(2) the only exception is char*, which allows you a "byte view of memory"

(3) calling a function through a pointer requires the parameter pointer types to be compatible, and none of these are: int*, struct foo *, void*, char*