`
yaojingguo
  • 浏览: 209051 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics