But is this code safe?
As I understand it, it will be safe if the uint16 len variables immediately follow the int8 array[] variables independently of other elements of the structure.
This is not safe in the sense that compilers are allowed to freely insert any number of additions between or after structure elements, so you cannot be sure that &ptr[2] points to the first byte of foo.array2 . However, if uint16 does have a width of two bytes (which is in no way guaranteed by the language), you can be sure that if size less than 25, then
memcpy ((void*)&ptr[2], (void*)str, size);
neither the bytes of foo.len2 nor the last byte of foo.array2 will be changed. Since foo was previously filled with zeros, this leaves foo.array2 as a correctly completed C string. Thus, you can safely print it with printf() . C, on the other hand, does not guarantee that the result of this will be the same as the result of printing str .
Will he only add paddings before uint16 when the size before it is odd?
This is at the discretion of the compiler. It can be influenced by pragmatics, command-line options, configuration options, language extensions (although none of them are used in this example), the target architecture, or anything else the compiler wants to use to make such decisions.
Is it used correctly?
As far as I can tell, the program is consistent if that is what you mean.
And more importantly, is it safe?
The output of the program is not predictable only from its code, therefore, in this sense, no, it is unsafe.