I was wondering if can be posible to access the actual member of a union by the union address and not by the ->
& .
operators. If that is posible, is that allowed?
问问题
415 次
1 回答
2
you can access the memory of the union using the addressof operation &(union)
e.g.
union foo {
int32_t bar;
int64_t baz;
} afoo;
void * address = &afoo;
But the point of a union is to allow transparent access to a 'shared' region of data that composed of different types; if you wanted char *
access to the data, then you can use a char *
member, and access the data in that manner.
pretending that the *address is a different type, we can use:
int64_t *abar = (int64_t*)address;
printf("%llu\n", *abar);
于 2012-06-07T04:59:12.460 回答