Access to internal union variable

I am reading the source code of Libevent2.0 and I find the code below which I cannot understand. Why can we access the element "ev_ncalls" without access to "_ev" and "ev_signal"? And I executed some codes like those that cannot go through gcc. Here is the code:

struct event { union { /* used for io events */ struct { TAILQ_ENTRY(event) ev_io_next; struct timeval ev_timeout; } ev_io; /* used by signal events */ struct { TAILQ_ENTRY(event) ev_signal_next; short ev_ncalls; /* Allows deletes in callback */ short *ev_pncalls; } ev_signal; } _ev; }; struct event *ev; int ncall = ev->ev_ncalls; 
+4
source share
1 answer

This is because event-internal.h has things like:

 #define ev_ncalls _ev.ev_signal.ev_ncalls #define ev_pncalls _ev.ev_signal.ev_pncalls 

So, when you say ev->ev_ncalls , the compiler sees ev->_ev.ev_signal.ev_ncalls .

+4
source

Source: https://habr.com/ru/post/1433512/


All Articles