You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
981 B

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.h"
// 학생생성 함수
Student* createStudent(const char* name, int id, float grade)
{
Student* newStudent = (Student*)malloc(sizeof(Student));
if (!newStudent)
{
printf("Fail to student malloc!\n");
exit(1);
}
strncpy(newStudent->name, name, sizeof(newStudent->name) - 1); // 가용한 마지막 문자까지 복사하고
newStudent->name[sizeof(newStudent->name) - 1] = '\0'; // 마지막\0 null문자 추가로 문자열 종료 보장
newStudent->id = id;
newStudent->grade = grade;
return newStudent;
}
// 학생정보 출력 함수
void printStudentInfo(const Student* student)
{
if (!student)
{
printf("Invalid student info.");
return;
}
printf("Name: %s, ID: %d, Grade: %.2f\n", student->name, student->id, student->grade);
}
void freeStudent(Student* student)
{
if (student)
free(student);
}