je dois mettre en œuvre ces fonctions dans internal.h ci-dessous:C allocation de mémoire dans la structure étudiante ne fonctionne pas
#ifndef STUDENT_INTERNAL_H
#define STUDENT_INTERNAL_H
#include "student.h"
/** Allocate memory for a new @ref student object. */
struct student *student_alloc(void);
/*** Initialize an already-allocated @ref student object.
* @returns NULL on failure but does not free the memory
*/
struct student *student_init(struct student *, const char *name, size_t namelen,
struct student_id, student_complete);
#endif /* STUDENT_INTERNAL_H */
C'est ce que j'ai à ce jour mais je suis confus et il ne fonctionne pas:
#include <sys/types.h>
#include "common.h"
#include "student.h"
#include "transcript.h"
#include "internal.h"
/**
* Initialize an already-allocated @ref student object.
* @returns NULL on failure but does not free the memory
*/
struct student *student_init(struct student *student, const char *name, size_t namelen,
struct student_id stud_id, student_complete stud_complete) {
struct student *initialized_student;
name = malloc(sizeof(namelen));
initialized_student = malloc(sizeof (student));
if (student_alloc()) {
initialized_student->s_name = name;
initialized_student->s_id = stud_id;
initialized_student->s_complete = stud_complete;
return initialized_student;
} else {
return 0;
}
}
stud_complete
est ici un pointeur de fonction qui est déclarée dans un fichier d'en-tête déférent de student.h comme typedef int (*student_complete)(struct student *);
Peut-être que ce serait une bonne idée d'apprendre à utiliser le débogueur –
'initialized_student = malloc (sizeof (étudiant))' est faux - >> ' initialized_student = malloc (sizeof (struct étudiant)); 'ou encore mieux:' initialized_student = malloc (sizeof * initialized_sudent); 'et name = malloc (sizeof (namelen));' est également faux. – wildplasser