Relationship between union and its members

998 단어
자세히 보기
Code:
#include 

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
", nsipcbuf->accept.req_s); } void from_union_to_member() { nsipcbuf = &shutdown; p = nsipcbuf; printf("%d
", nsipcbuf->accept.req_s); } int main(int argc, const char *argv[]) { from_member_to_union(); from_union_to_member(); return 0; }
 
 
Output:
100
1

좋은 웹페이지 즐겨찾기