parent
b65491612f
commit
d38063dd9d
@ -0,0 +1,19 @@ |
|||||||
|
#include <stdio.h> |
||||||
|
#include "student.h" |
||||||
|
|
||||||
|
int main() |
||||||
|
{ |
||||||
|
Student* student1 = createStudent("Bill Gates", 1001, 95.5); |
||||||
|
Student* student2 = createStudent("Steve Jobs ", 1002, 88.0); |
||||||
|
|
||||||
|
printStudentInfo(student1); |
||||||
|
printStudentInfo(student2); |
||||||
|
|
||||||
|
freeStudent(student1); |
||||||
|
freeStudent(student2); |
||||||
|
|
||||||
|
printStudentInfo(student1); |
||||||
|
printStudentInfo(student2); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
#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); |
||||||
|
} |
@ -0,0 +1,20 @@ |
|||||||
|
#ifndef STUDENT_H |
||||||
|
#define STUDENT_H |
||||||
|
|
||||||
|
typedef struct |
||||||
|
{ |
||||||
|
char name[50]; |
||||||
|
int id; |
||||||
|
float grade; |
||||||
|
} Student; |
||||||
|
|
||||||
|
// 학생 생성 함수
|
||||||
|
Student* createStudent(const char* name, int id, float grade); |
||||||
|
|
||||||
|
// 학생정보 출력 함수
|
||||||
|
void printStudentInfo(const Student* student); |
||||||
|
|
||||||
|
// 학생 메모리 해제 함수
|
||||||
|
void freeStudent(Student* Student); |
||||||
|
|
||||||
|
#endif |
Loading…
Reference in new issue