Just trying to make a simple toString function but I'm having difficulty figuring out how I can create an sprintf style string without having to create a temporary variable.
i.e. It would be wonderful if the following worked
#myfile.c
bool status;
int state;
...
const char* myfileToString(void) {
return sprintf("myfile::status=%s;state=%d;", \
status ? "true" : "false", \
state);
}
this won't work since sprintf needs you to first create the string then pass it to the function.
i.e. I believe the following works
#myfile.c
bool status;
int state;
...
const char* myfileToString(void) {
char ret[40];
sprintf(ret, \
"myfile::status=%s;state=%d;", \
status ? "true" : "false", \
state);
return ret;
}
is there another function that I can use instead of sprintf or can I do something weird with sprintf like passing it some pointer to return directly sprintf(*return, "...", ...);
asprintf, there are portable sources for it if your platform does not provide it.