I have a struct that contains a string and a length:
typedef struct string {
char* data;
size_t len;
} string_t;
Which is all fine and dandy. But, I want to be able to output the contents of this struct using a printf-like function. data may not have a nul terminator (or have it in the wrong place), so I can't just use %s. But the %.*s specifier requires an int, while I have a size_t.
So the question now is, how can I output the string using printf?
size_tto anint, provided the value fits...intthat's going to be one interestingprintfcall :-)) Should check though since it could overflow.datamight contain non-printable characters (like a null character), you don't want%sat all. Write a loop.