#define _GNU_SOURCE // really not portable! #include typedef struct student_st { char * name; char * major; int id; } Student; char * student_toString(Student *s) { char *str; if ( asprintf(&str, "Name: %s\nMajor: %s\nId: %d", s->name, s->major, s->id ) < 0 ) return NULL; return str; } int main(int argc, char *argv[]) { Student example = { .name = "Alice", .major = "CSE", .id = 1 }; printf("The example student:\n%s\n", student_toString(&example)); return 0; }