Given the requirements, it looks as though this could work, as mentioned in my comment:
char *serialize_struct_t(const struct_t structure, char *s, int len)
{
if (snprintf(s, len, "[%u,%u,%u,%u]", structure->a, structure->b,
structure->c, structure->d) < len)
return s;
return 0;
}
This produces output such as [23,45,67,90]. Clearly, there are many other possible formats, with blanks as separators and without the beginning or end markers. Any such changes will require consequential changes in the deserialization code shown below.
Assuming that you use the format shown above, you might then deserialize such data using a function like:
int deserialize(const char *buffer, struct_t *structure)
{
char c;
int len;
if (sscanf(buffer, " [%u ,%u ,%u ,%u %c%n", &structure->a,
&structure->b, &structure->c, &structure->d, &c, &len) != 5 ||
c != ']')
return -1;
return len;
}
You can debate the details of the interface, but it returns -1 if the string doesn't match the output from serialize_struct_t(), or the length of the string which did match (so you know where to continue searching from). The blanks in the format indicate optional white space; %u skips optional white anyway. The %c allows you to detect something other than a ] at the end; you'd not be able to spot a problem if you used a litera ] in the format string. And the %n tells you the offset where it is matched, which is at the end. The %n conversion isn't counted.
Clearly, depending on the serialization format that's chosen, you will need to alter the deserialization scanner to match.
This is the way serialization and deserialization work — the serialization format (which should be strictly defined) controls what the deserialization code needs to recognize. The deserialization code can be more flexible (you could argue against all the blanks in the format string shown, for example, but that would make the accepted format more rigid), but should certainly recognize anything that the serialization code produces. See Wikipedia on the Robustness Principle, aka Postel's Law.
snprintfto write the string.snprintf(buf, len, "[%u,%u,%u,%u]", structure->a, structure->b, structure->c, structure->d)might be sufficient (if you check that the output was not truncated — capture and test the return value fromsnprintf()).char * struct_to_string (struct_t* structure, char* s, int len);This function creates the representation of the structure as a string. \retval must be s if the conversion has been successful, otherwise \retval must be NULL. I'm working on a project which requires both the conversion of a string (call it string[i]) into a structure (done, it's working fine) and then again the conversion of this structure into a new string, which obviously will be basically the same of string[i].