一直以来很像tcp一样,做可变长的网络数据包,为此自己写了一些很有意思的测试代码,想了解联合体的朋友,不妨从我这里拷过去自己亲自调试调试。
#include <iostream.h>
#include <stdlib.h>
#include <memory.h>
struct Msg_t
{
char a;
int b;
int c;
};
struct Msg_b
{
char a;
char b;
char c;
char d;
};
struct stU
{
int id;
union
{
int a;
struct Msg_b msgb;
struct Msg_t msgt;
};
};
void main()
{
struct stU st;
st.id = 1;
st.msgb.a = 'A';
st.msgb.b = 0;
st.msgb.c = 0;
st.msgb.d = 0;
cout<<sizeof(struct stU)<<endl;
cout<<st.a<<endl;
}