我一直试图弄清楚以下是合法的,我真的可以使用一些帮助.
#include <stdio.h>
#include <stdlib.h>
typedef struct foo {
int foo;
int bar;
} foo;
void make_foo(void * p)
{
foo * this = (foo *)p;
this->foo = 0;
this->bar = 1;
}
typedef struct more_foo {
int foo;
int bar;
int more;
} more_foo;
void make_more_foo(void * p)
{
make_foo(p);
more_foo * this = (more_foo *)p;
this->more = 2;
}
int main(void)
{
more_foo * mf = malloc(sizeof(more_foo));
make_more_foo(mf);
printf("%d %d %d\n", mf->foo, mf->bar, mf->more);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这样做就是打字,并且应该违反严格的别名规则.不过吗?传递的指针无效.您可以按照自己的意愿解释无效指针,对吗? …