#define _GNU_SOURCE #include #include #include #include "student_v1.h" static Student *_student_init(Student *s, const char *name, unsigned int id) { s->name = strdup(name); if ( s->name == NULL ) return NULL; s->id = id; return s; } Student *student_new(const char *name, unsigned int id) { Student *s = malloc(sizeof(Student)); if (!s) { return NULL; } if ( _student_init(s, name, id) == NULL ) { free(s); return NULL; } return s; } int student_delete(Student *s) { if (s==NULL) return -1; free(s->name); free(s); return 0; // Assume it always works :) } char *student_toString(const Student *s) { char *str = NULL; if (s==NULL) return NULL; if (asprintf(&str, "Name: %s\n", s->name) <= 0) return NULL; return str; } const char *student_get_name(const Student *s) { if (s==NULL) return NULL; return s->name; } unsigned int student_get_id(Student *s) { return s->id; } int student_set_id(Student *s, unsigned int new_id) { if (s == NULL) return -1; s->id = new_id; return 0; }