포인터 주소값 관련해서 질문.

decadant의 이미지

취직때문에

부랴부랴 c 언어 4일째 공부중입니다.

그전에 자바 아주 얇게 공부했습니다.

아 왜이렇게 답답한지ㅋ 하루종일 고민했는데 안풀려서

고수님의 현안을 듣고 싶습니다.

제 사수가 내준 과제입니다.

간단하게 text 파일에

hong 10 seoul
kim 20 busan
lee 30 incheon 저장되어있고

이름을 쳤을 때 그 이름이 속한 행을 출력하는 프로그램입니다.

쑥쓰럽지만;;

1 #include
2 #include
3
4 struct member{
5 char name[10];
6 int age;
7 char city[10];
8 };
9
10 int main(){
11
12 char name[10];
13 int i, menu;
14 struct member m[3];
15 FILE* fp = fopen("input.txt","r");
16
17 while(1)
18 {
19 fseek(fp, 0, SEEK_SET);
20
21 printf("[MENU]\n");
22 printf("1. Search NAME 2. END\n\n");
23
24 printf("Select Name: ");
25 scanf("%d", &menu);
26
27 if(menu == 1)
28 {
29 printf("\nNAME: ");
30 scanf("%s", name);
31
32 for(i=0;i<3;i++)
33 {
34 fscanf(fp,"%s %d %s", m[i].name, &m[i].age, m[i].city);
35
36
37 if(strcmp(name, m[i].name) == 0);
38 break;
39 }
40 if(i!=3)
41
42 printf("\n%s %d %s\n\n", m[i].name, m[i].age, m[i].city);
43 }
44 else if(menu ==2)
45 {
46 break;
47 }
48 }
49 fclose(fp);
50
51 return 0;
52 }

컴파일은 문제 없으나 실행시

Segmentation fault (core dumped)

이런게 뜨더군요

gcc -Wformat 으로 보면

find.c: In function 'main':
find.c:34: warning: format '%s' expects type 'char *', but argument 3 has type 'int'
find.c:34: warning: format '%s' expects type 'char *', but argument 5 has type 'int'

34행에 비교하는 변수 타입이 잘못된거 같으나;;

고민을 해봐도 풀리지 않습니다.

안그래도
34행의 & 라던가 문자열 포인터 관련해서 뭔가가 잘못된거 같은데

현안을 부탁드립니다.

File attachments: 
첨부파일 크기
Image icon 2.JPG61.22 KB
익명 사용자의 이미지

34행 다음에 각 멤버의 값들을 출력해보세요.

decadant의 이미지

출력해보니 무슨 이름을 넣더라도

hong 25 seoul 이 나오더군요;;

skate2의 이미지

저거 때문에 break가 if랑 상관 없이 계속실행되는 것 같아요

익명 사용자의 이미지

제가 만든다면, 이렇게 짤 듯 한데요...

if(strcmp(name, m[i].name) == 0)
{
printf( 이름/나이/도시 출력 );
break;
}

if( i >= 3 )
{
break;
}

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.