я должен выполнять эти функции в internal.h ниже:C выделения памяти в структуре студентов не работает
#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 */
Это то, что я до сих пор, но я смущен, и это не работает:
#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
здесь указатель функции, которая объявлена в заголовочном файле отводящий student.h как typedef int (*student_complete)(struct student *);
Возможно, это было бы хорошей идеей, чтобы узнать, как использовать отладчик –
'initialized_student = таНос (SizeOf (студент))' неправильно - >> ' initialized_student = malloc (sizeof (struct student)); 'или даже лучше:' initialized_student = malloc (sizeof * initialized_sudent); 'и' name = malloc (sizeof (namelen)); 'также неверно. – wildplasser