in example code generated by rpcgen (appname_server.c), there was union used as “define”. it was simmilar to this:
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define alignof _Alignof
#define LOG(d) printf("> name: %5s, value: %8X, sizeof: %zu, alignof: %zun", #d, *(int*)&d, sizeof(d), alignof(d));
typedef int // or other type
very_very_special_int_1,
very_very_special_int_2;
int
main(int argc, char** argv)
{
/**/// ======================
// union --------------------
union {
very_very_special_int_1 very_very_special_int_1_variable; // descriptive/special name
} foo; // new name, "define"
memcpy(&foo, &(int){0xDEAD}, sizeof(int)); // some operations using new name, (passing by pointer)
// struct --------------------
struct {
very_very_special_int_2 very_very_special_int_2_variable; // descriptive/special name
} bar; // new name, "define"
memcpy(&bar, &(int){0x7EC}, sizeof(int)); // some operations using new name, (passing by pointer)
LOG( foo );
LOG( bar );
// */// ======================
/* output --------------------
> name: foo, value: DEAD, sizeof: 4, alignof: 4
> name: bar, value: 7EC, sizeof: 4, alignof: 4
*/
return 0;
}
have you evered used it in your code? i dont see many advantages unless you are a code generator.
1