I tried to create a sort of toString function in C like this:
struct esploratore {
char nome[20];
char cognome[20];
int nascita;
int morte;
};
char *esploratoreToString(esploratore e) {
char *s = (char*) malloc(50 * sizeof(char));
snprintf(s, 50,
"%s %s %d %d",
e.nome, e.cognome, e.nascita, e.morte);
return s;
}
and use it in main this way:
printf("%s", esploratoreToString(e));
it works, but the problem is: will the memory assigned by the malloc be ever freed?
is this the only solution?
char *s = esploratoreToString(e);
...
printf("%s\n\n", s);
free(s);