S
s0suk3
Seems like it's a bit hard to reason with Richard, so let's move onto
practice a bit...
Coincidentally, just a while ago I found myself using snprintf() in a
program to turn a number into a string:
...
size_t serviceLen = 5;
char *service = malloc(sizeof(char) * serviceLen);
if (service == NULL)
return FTT_ERROR_OUT_OF_MEMORY;
while (snprintf(service, serviceLen, "%d", port) >= serviceLen) {
serviceLen += 5;
char *new = realloc(service, sizeof(char) * serviceLen);
if (new == NULL) {
free(service);
return FTT_ERROR_OUT_OF_MEMORY;
}
service = new;
}
...
Never mind. The approach described by CBFalconer is much better. This
is what it would boil down to:
....
int serviceLen = snprintf(NULL, 0, "%d", port);
char service[serviceLen + 1];
snprintf(service, serviceLen, "%d", port);
....
Sebastian