The two structs in your post don't have the same structure at all. h1
has a integer and a pointer to char. h2
has an integer, and an array of characters inline (number of elements determined at runtime, possibly none).
Said differently, in h2
the character data is inside the struct. In h1
it has to be somewhere outside.
This makes a lot of difference. For instance, if you use h1
you need to take care of allocating/freeing the payload (in addition to the struct itself). With h2
, only one allocation/free is necessary, everything is packaged together.
One case where using h2
might make sense is if you're communicating with something that expects messages in the form of {length,data}
pairs. You allocate an instance of h2
by requesting sizeof(h2)+how many payload chars you want
, fill it up, and then you can transfer the whole thing in a single write
(taking care about endianess and such of course). If you had used h1
, you'd need two write
calls (unless you want to send the memory address of the data, which usually doesn't make any sense).
So this feature exists because it's handy. And various (sometimes non-portable) tricks where used before that to simulate this feature. Adding it to the standard makes sense.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…