테스트를 위해 사용할 자료는 회원 번호와 이름을 멤버로 갖는 회원 데이터로 할게요.
typedef struct _Member Member; #define MAX_NAME_LEN 20 struct _Member { int mnum; char name[MAX_NAME_LEN+1]; };
그리고 동적으로 생성하는 함수, 소멸하는 함수, 출력하는 함수를 제공하세요.
◈ Member.h
#pragma once typedef struct _Member Member; #define MAX_NAME_LEN 20 struct _Member { int mnum; char name[MAX_NAME_LEN+1]; }; Member *NewMember(int mnum,const char *name); void DeleteMember(Member *member); int MemberGetNum(Member *member); const char *MemberGetName(Member *member); void MemberView(Member *member);
회원 데이터를 동적으로 생성하는 함수에서는 먼저 동적으로 Member 형식 크기의 메모리를 할당받아야겠죠.
그리고 동적으로 생성한 Member 메모리의 멤버의 초기값을 입력 인자로 전달받은 값으로 설정하세요.
void MemberMember(Member *member,int mnum,const char *name); Member *NewMember(int mnum,const char *name) { Member *member = 0; member = (Member *)malloc(sizeof(Member)); MemberMember(member,mnum,name); return member; } void MemberMember(Member *member,int mnum,const char *name) { member->mnum = mnum; memset(member->name,0,sizeof(member->name)); strncpy(member->name,name,MAX_NAME_LEN); }
동적으로 생성한 회원 데이터를 소멸하는 함수에서는 자신의 메모리를 해제하세요.
void DeleteMember(Member *member) { free(member); }
그리고 회원 데이터를 콘솔 화면에 출력하는 함수도 제공하세요.
void MemberView(Member *member) { printf("번호:%d 이름:%s\n",member->mnum,member->name); }
◈ Member.c
#include "Member.h" #include <stdlib.h> #include <string.h> #include <stdio.h> void MemberMember(Member *member,int mnum,const char *name); Member *NewMember(int mnum,const char *name) { Member *member = 0; member = (Member *)malloc(sizeof(Member)); MemberMember(member,mnum,name); return member; } void MemberMember(Member *member,int mnum,const char *name) { member->mnum = mnum; memset(member->name,0,sizeof(member->name)); strncpy(member->name,name,MAX_NAME_LEN); } void DeleteMember(Member *member) { free(member); } void MemberView(Member *member) { printf("번호:%d 이름:%s\n",member->mnum,member->name); }