The following small program compiles on gcc and runs fine:
#include <stdio.h>
#include <stdlib.h>
typedef struct foo dne;
int main(int argc, char *argv[]) {
dne *dne_var = malloc(sizeof(void*));
printf("%p\n", dne_var);
return 0;
}
Why is the typedef valid?
The line
typedef struct foo dne;
implicitly declares an (incomplete at this point) structure struct foo
. A pointer to an incomplete type is a complete type, so e.g. it’s size is known and you can declare an object of that type. However, struct foo
itself isn’t complete until you provide a full declaration of it, so e.g.
dne dne_var;
or dereferencing your pointer to access the fields of the structure would be invalid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With