defensive-coding-guide/modules/ROOT/examples/C-String-Functions-snprintf-incremental.adoc

11 lines
262 B
Text
Raw Normal View History

char buf[512];
char *current = buf;
const char *const end = buf + sizeof(buf);
for (struct item *it = data; it->key; ++it) {
snprintf(current, end - current, "%s%s=%d",
current == buf ? "" : ", ", it->key, it->value);
current += strlen(current);
}