There is no guarantee that two different C compilers will use the same representation for the same type β unless they both comply with some external standard (ABI) that describes the representation in sufficient detail.
Most likely, the filling difference is when one compiler requires double be 4 bytes and the other requires that it be aligned by 8 bytes. Both options are great for C and C ++ standards.
You can learn more about this by printing the sizes and offsets of all members of your structure:
printf("nbranch: size %3u offset %3u\n", (unsigned)sizeof tree.nbranch, (unsigned)offsetof(struct TB, nbranch));
There may be a compiler-specific way to specify a different alignment, but such methods are not always safe .
The ideal solution is to use the same compiler for C and C ++ code. C is not a subset of C ++, but as a rule, it should not be too difficult to modify existing C code, so it compiles as C ++.
Or, you may be able to change your definition of structure so that both compilers are in the same way. First, you can use the double element. This is still not guaranteed, and it may break with future versions of any compiler, but it is probably good enough.
Do not forget that at the very end of the structure there may also be a gasket; this is sometimes necessary to ensure proper alignment for arrays of structures. Take a look at sizeof (struct TB) and compare it with the size and offset of the last declared element.
Another option: Insert explicit, unused elements to force alignment. For example, suppose you have:
struct foo { uint16_t x; uint32_t y; };
and one compiler puts y in 16 bits, and the other puts it in 32 bits with 16 bits of padding. If you change the definition to:
struct foo { uint16_t x; uint16_t unused_padding; uint32_t y; };
then you are more likely to have x and y tags for both compilers. You still have to experiment to make sure everything is consistent.
Since the C and C ++ code will be part of the same program (right?), You donβt have to worry about things like changing the byte order. If you want to transfer the values ββof your structure type between separate programs, say, saving them in files or transferring them over the network, you may need to define a sequential way to serialize the structure value into a sequence of bytes and vice versa.