Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
In addition to all the examples I gave in my answer to the linked post, all scenarios where p is a pointer to incomplete type fails. Not just the void* scenario, but also when p is a pointer to an ...
Answer
#1: Initial revision
In addition to all the examples I gave in my answer to the linked post, all scenarios where `p` is a pointer to incomplete type fails. Not just the `void*` scenario, but also when `p` is a pointer to an array of incomplete type: int (*p)[] = malloc(n * sizeof *p); // will not compile cleanly And also when using forward-declared structs/unions: struct fwd_declared* p; p = malloc(n * sizeof *p); // will not compile cleanly And when using function pointers: void (*p)(void); p = malloc(n * sizeof *p); // will not compile cleanly --- All of these examples (including de-referencing `void` pointers) are invalid C as per C17 6.5.3.4: > **Constraints** > > The `sizeof` operator shall not be applied to an expression that has function type or an incomplete type Regarding "compile cleanly" please see [What must a C compiler do when it finds an error?](https://software.codidact.com/posts/277340) And please note that gcc/clang are by default in a non-standard mode until you pass along `-std=c17 -pedantic-errors` to tell them to become conforming implementations. --- These are weak arguments against using a certain `malloc` style though.