[초보]아래꺼 잘못 올려서 다시 올립니다.

woosub21의 이미지

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
typedef struct infomation{
	char name[20];
	char phone[30];
}info;//이름과 전화번호를 저장할 구조체
 
 
int menu()//메뉴 출력 함수
{
	printf("*****Menu*****\n");
	printf("<1.Registration><2.ShowAll><3.Search>\n");
	printf("<4.Deletion><5.Modification><6.Exit>\n");
	printf("Enter_the_menu_number:");
	return 0;
}
int get_menu_num()//메뉴번호 선택 함수
{
	int n;
	scanf("%d",&n);
	return n;
}
 
int input(int *i,info* in[1000])//초기 입력 함수
{
	(in[*i])=(info*)malloc(sizeof(info));
	printf("Name:");
	scanf("%s",in[*i]->name);
	printf("Phone_number:");
	scanf("%s",in[*i]->phone);
	*i = *i + 1;
	return 0;
}
 
int output(int *i,info* in[1000])//출력 함수
{
	int l;
	for(l=0;l<*i;l++){//입력된 순서대로 출력
		printf("Name:%s--->Phone_number:%s--->address:%lld\n",in[l]->name,in[l]->phone,&in[l]);
	}
	return 0;
}
 
int search(int *i,info* in[1000])//검색함수
{
	int l;
	char search_name[20];
 
	printf("Name:");
	scanf("%s",search_name);
 
	for(l=0;l<*i;l++)//입력한 값과 저장된값을 비교후 같은 값이 있으면 이름과 번호 출력
	{
		if(strcmp(search_name,in[l]->name) == 0){
		printf("Name:%s--->Phone_number:%s--->address:%lld\n",in[l]->name,in[l]->phone,&in[l]);
		break;
		}
	}
	return 0;
}
 
 
int del(int *i,info* in[1000])//삭제함수
{
	int l,k,p=0;
	char del_name[20];
	info* null;
	printf("Name:");
	scanf("%s",del_name);
	for(l=0;l<(*i);l++){//검색한 이름이 있는지 확인
		if(strcmp(del_name,in[l]->name)==0){
			for(k=l;k<(*i)-1;k++){//이름이 있다면 다음 배열들을 한칸씩 앞으로 복사
				strcpy(in[k]->name,in[k+1]->name);
				strcpy(in[k]->phone,in[k+1]->phone);
				p=k;
			}
			free(in[p+1]);//배열 복사후 마지막 메모리를 초기화
			in[p+1]=in[p+2];
			*i = (*i)-1;
			return 0;
		}
		else
			continue;
	}
	return 0;
}
 
int modify(int *i,info* in[1000])//수정 함수
{
	int l;
	char mody_name[20];
	char new_phone[30];
 
	printf("Name:");
	scanf("%s",mody_name);
 
	for(l=0;l<(*i);l++){//검색한 이름이 있는지 확인
		if(strcmp(mody_name,in[l]->name)==0){//있다면 수정할 전화번호 입력받음
			printf("New_Phone_number:");
			scanf("%s",new_phone);
			strcpy(in[l]->phone,new_phone);//입력받은 전화번호를 원래 배열에 복사
			break;
		}
	}
	return 0;
}
 
int main()
{
	int i=0;
	info* information[1000];
 
	while(1){
		menu();
		switch(get_menu_num()){
		case 1:
			input(&i,information);
			break;
		case 2:
			output(&i,information);
			break;
		case 3:
			search(&i,information);
			break;
		case 4:
			del(&i,information);
			break;
		case 5:
			modify(&i,information);
			break;
		case 6:
			return 0;
		}
	}
} 

전화번호와 이름을 저장하는 프로그램이고요
구조체 포인터 배열이랑 malloc을 이용해서 만들어야 합니다.
이렇게 코드를 짜면 프로그램은 잘 돌아가는데
막상 채점 프로그램(리눅스기반)에 넣으면 segmentation fault가 뜨더군요 ㅠ

무얼 어떻게 수정해야될지 감이 안와서 ㅠㅠ

snowall의 이미지

info *in[1000]이 자꾸 거슬리는데 이거 왜 그렇게 한거죠?

피할 수 있을때 즐겨라! http://melotopia.net/b

shint의 이미지

- 변수'에 크기를 할당하고. 읽고. 사용하는 방법에 대한 이해가 필요해 보입니다.
- 변수의 초기화와 {}의 구분을 잘 구분해주는것이 좋습니다.
- 되도록 변수명은 비슷하지 않게. 지어줘야 합니다.
- segmentation fault 는 배열의 범위를 넘어갈때 발생하는것으로 알고 있습니다.
- 현재 코드는 free()를 실행할때. 액세스 위반이 발생합니다.

int a;      //int형 변수 a
int aa[10]; //int형 변수 a가 10개 있는 배열
 
int* b;     //int형 포인터 변수 b
int* bb[10];//int형 포인터 변수 b가 10개 있는 배열
 
info* information[1000]; //info형 포인터 변수 information이 1000개 있는 배열
 
 
//
int del(int *i,info* in[1000])//삭제함수
{
	int l = 0;
	int k = 0;
	int p = 0;
	char del_name[20];
	info* null=NULL;   //사용하지 않는 변수는 제거해주고. 포인터인경우. NULL로 초기화 해줍니다.
        memset(del_name, 0x00, sizeof(del_name));
 
	printf("Name:");
	scanf("%s",del_name);
 
	for(l=0;l<(*i);l++)//검색한 이름이 있는지 확인
	{
		if(strcmp(del_name,in[l]->name)==0)
		{
			for(k=l;k<(*i)-1;k++)//이름이 있다면 다음 배열들을 한칸씩 앞으로 복사
			{
				strcpy(in[k]->name,in[k+1]->name);
				strcpy(in[k]->phone,in[k+1]->phone);
				p=k;
			}
			//l = 1 *i = 3 p = 1 k = 2
			printf("l = %d *i = %d p = %d k = %d\n", l, *i, p, k);
			free(in[k]);//배열 복사후 마지막 메모리를 초기화
			*i = (*i)-1;
			return 0;
		}
	}
	return 0;
}

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

익명 사용자의 이미지

왜 세그먼테이션 폴트가 날 때 발생하는 메시지는 안적으시죠? 어느 라인에서 발생하는지가 중요한 정보인데요.
예를 들어 ptr->name이라는 코드에서 ptr 이 NULL 이면 발생합니다. 왜 그렇게 되는지 디버깅해보세요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.