Relationship between union and its members

Code:

#include <stdio.h>

struct Nsreq_accept {
	int req_s;
};

struct Nsreq_shutdown {
	int req_s;
	int req_how;
};

union Nsipc {
	struct Nsreq_accept accept;
	struct Nsreq_shutdown shutdown;
};

union Nsipc* nsipcbuf;
struct Nsreq_accept* p;

struct Nsreq_accept accept = { 100 };
struct Nsreq_shutdown shutdown = { 1, 2};

void from_member_to_union() {
	nsipcbuf = &accept;
	printf("%d\n", nsipcbuf->accept.req_s);
}

void from_union_to_member() {
	nsipcbuf = &shutdown;
	p = nsipcbuf;
	printf("%d\n", nsipcbuf->accept.req_s);
}

int main(int argc, const char *argv[]) {
	from_member_to_union();
	from_union_to_member();
	
	return 0;
}
 

 

Output:

100
1
 

你可能感兴趣的:(UNION)