구조체와 malloc에 대해..제 소스보고 사용법 좀 알려주세요
봄나
계속 개념찾아보면서 만들고 있는데
오류만 나네요.ㅠ
#includestdio.h
#includestring.h
#includestdlib.h
struct telephone{
char* hoem_tel;
char* mobile_tel;
};
struct address{
char* name;
char* add;
struct telephone tel;
};
void desplay_tel(struct address temp);//전화번호보기
void desplay_all(struct address temp[]);//전체보기
int desplay_menu();//메뉴보기
void input_address(struct address temp[]);//주소 넣기
void search_name(char *name,struct address temp[]);//이름으로 검색
void search_tel(char *tel,struct address temp[]);//전화번호로 검색
int COUNT=0;//전화번호에 저장한 갯수
char* mallo_temp = {0,};//임시로 저장할 변수
int main(){
struct address telbook[10];//저장 가능한 전화번호
char* temp = {0,};//임시로 메뉴를 저장
int num;
while(1){
desplay_menu();
printf(메뉴를 입력하세요 : );
scanf(%d,&num);;
switch (num){
case 1://입력
input_address(telbook); COUNT++;break;
case 2://이름으로 찾기
printf(찾을 이름을 입력하세요 : );
scanf(%s,&temp);
search_name(temp,telbook);
break;
case 3:// 전화번호로 찾기
printf(전화번호를 입려하세요 : );
scanf(%s,&temp);
search_tel(temp,telbook);break;
case 4:// 모두 출력
desplay_all(telbook);break;
case 5: //종료
printf(프로그램을 종료합니다!!); return 0;
default : printf(입력이 잘못 됐습니다!!\n);
}//end_switch
fflush(stdin);
}//end_while
}//end_main
//전화번호 보기
void desplay_tel(struct address temp){
printf(이름 : %s \n,temp.name);
printf(주소 : %s \n,temp.add);
printf(집전화 : %s \n,temp.tel.hoem_tel);
printf(핸츠폰 : %s \n,temp.tel.mobile_tel);
}
//모두 보기
void desplay_all(struct address temp[]){
int i;
for(i = 0;iCOUNT;i++){
printf(----------%d 번째 주소록-------- \n,i+1);
printf(이름 : %s \n,temp[i].name);
printf(주소 : %s \n,temp[i].add);
printf(집전화 : %s \n,temp[i].tel.hoem_tel);
printf(핸드폰 : %s \n,temp[i].tel.mobile_tel);
}
}
//메뉴 보기
int desplay_menu(){
printf(---------------menu--------------\n);
printf(1. 전화번호 입력(%d/10)\n,COUNT);
printf(2. 이름으로 입력\n);
printf(3. 전화번호 찾기\n);
printf(4. 모두 출력\n);
printf(5. 종료\n);
printf(---------------------------------\n);
return 0;
}
//전화번호 입력
void input_address(struct address temp[]){
printf(이름을 입력하세요 : );
scanf(%s, &mallo_temp);
temp[COUNT].name = (char*)malloc(sizeof(char) * strlen(mallo_temp)+1);
strcpy(temp[COUNT].name,mallo_temp);
mallo_temp = NULL;
printf(\n주소를 입력하세요 : );
scanf(%s, &mallo_temp);
temp[COUNT].add = (char*)malloc(sizeof(char) * strlen(mallo_temp)+1);
strcpy(temp[COUNT].add,mallo_temp);
mallo_temp = NULL;
printf(집전화번호를 입력하세요 : );
scanf(%s, &mallo_temp);
temp[COUNT].tel.hoem_tel = (char*)malloc(sizeof(char) * strlen(mallo_temp)+1);
strcpy(temp[COUNT].tel.hoem_tel,mallo_temp);
mallo_temp = NULL;
printf(\n핸드폰전화번호를 입력하세요 : );
scanf(%s, &mallo_temp);
temp[COUNT].tel.mobile_tel = (char*)malloc(sizeof(char) * strlen(mallo_temp)+1);
strcpy(temp[COUNT].tel.mobile_tel,mallo_temp);
free(mallo_temp);
}
//이름으로 찾기
void search_name(char *name,struct address temp[]){
int i;
for(i = 0;i10;i++){
if(strcmp(name,temp[i].name)){
}else{desplay_tel(temp[i]);
}
}
}
//전화번호로 찾기
void search_tel(char *tel,struct address temp[]){
int i;
for(i = 0;i10;i++){
if(strcmp(tel,temp[i].tel.hoem_tel)){
}else{desplay_tel(temp[i]);
}//end_if-else
}//end_for
}
하는법하고
해석 좀 부탁드려요~
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700562 | 함수포인터에서요 (7) | 소심한여자 | 2025-07-06 |
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |
2700426 | 인터넷 창 띄우는 질문이요 (1) | 정훈 | 2025-07-04 |
2700400 | 원넓이를 계산이요 ㅜㅜ | 천칭자리 | 2025-07-04 |
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |