The two structures in your message do not have the same structure at all. h1 has an integer and a pointer to char. h2 has an integer and an inline character array (the number of elements determined at run time may not).
In other words, in h2 character data is inside the structure. In h1 it must be somewhere outside.
This is of great importance. For example, if you use h1 , you need to take care of allocating / releasing the payload (in addition to the structure itself). With h2 only one distribution is required / free, everything is packed together.
One case where using h2 might make sense is that you are communicating with what is expecting a message in the form of {length,data} pairs. You assign an instance of h2 , asking for sizeof(h2)+how many payload chars you want , populate it, and then you can pass it all in one write (taking care of the entiance and, of course, that). If you used h1 , you will need two calls to write (if you do not want to send the address of the data memory, which usually does not make sense).
So, this function exists because it is convenient. And the various (sometimes not portable) tricks that were used before to imitate this feature. Adding it to the standard makes sense.
source share