2013-12-14 3 views
0

나는 C 언어를 연습하고 입력 한 요일이 목록에 있는지 알려주는 구조로 연결된 목록을 만들려고합니다. C 연결된 목록 포인터 문제

#include <stdio.h> 
#include <stdbool.h> 

bool isTrue=1, *ptrisTrue=&isTrue; 
struct weekday { 
    char *ptrday; 
    struct weekday *next; 
} sunday, monday, tuesday, wednesday, thursday, friday, saturday; 
struct weekday *head=&sunday; 
struct weekday *cursor; 
struct weekday *ecursor; 

void matchtest(char *eday, struct weekday *head, struct weekday *cursor) { 
    cursor=head; 
    while (cursor!=(struct weekday *)0){ 
     while (*eday!='\0') { 
      if (*eday!=*cursor->ptrday) 
      *ptrisTrue=0; 
      ++eday; ++cursor->ptrday; 
     } 
     if (*ptrisTrue==1) 
      printf("Yes, %s is in the list\n", cursor->ptrday); 
     cursor=cursor->next; 
    } 
} 

int main (void) { 
    char enteredday[]="Monday", *ptreday=enteredday; 
    sunday.ptrday="Sunday"; monday.ptrday="Monday"; tuesday.ptrday="Tuesday"; 
     wednesday.ptrday="Wednesday"; thursday.ptrday="Thursday"; 
     friday.ptrday="Friday"; saturday.ptrday="Saturday"; 

    sunday.next=&monday; monday.next=&tuesday; tuesday.next=&wednesday; 
     wednesday.next=&thursday; thursday.next=&friday; friday.next=&saturday; 
     saturday.next=(struct weekday *)0; 
     head->next=&sunday; 


    printf("This is a test to see if a day is in the list.\n"); 
    matchtest(ptreday, head, cursor); 

    return 0; 
} 

는 이 프로그램은 아무데도 가장 효율적인 하나의 근처 (나는. 지금이 월요일로 설정을 위해 ", enteredday"에 대한의 스캔 기능을 넣어 것입니다)하지만, 난 그냥 다른 개념을 테스트하고 그 나는 이미 배웠다. 중단 점을 사용하여 프로그램의 문제점을 찾아내는 경우 커서가 "matchtest"함수의 첫 번째 while 문 끝에서 다음 구조를 가리 키도록 설정할 때 표시됩니다 (cursor = cursor-> next;), 구조의 날 구성원에 대한 커서 값은 "월요일"대신 두 개의 따옴표 ("")로 설정됩니다. 이 문제를 어떻게 해결할 수 있습니까?

+0

먼저, 첫 번째 불일치에서'isTrue'를 0으로 설정하고 코드의 다른 곳에서는 결코 0이 아닌 값을 설정하지 마십시오. –

+1

'bool' 변수는 일반적으로'false' ('0'보다 우선) 또는'true' ('1'보다 우선)로 지정되어야합니다. ''트루 (Truje) '라는 이름은 조금이라도 마음을 불허합니다. 무슨 말이 사실입니까? 그리고 '거짓말 쟁이'는 여전히 마음이 굽혀 있습니다. 때때로 isFalse에서 repoint 할 필요가 있습니까? 그것은 명명법을 걱정하고있다. –

+1

나는 Jonathan에 동의한다. 나는 단지 미래의 이슈로 이어질 수있는 글로벌 변수를 정의하는 것을 추가 할 것이고, 글로벌하게 그것들을 "모든"프로그램으로 설정하는 것은별로 의미가 없다. 게다가, 솔직히 말하면, 여러분의 코드는 a와 b, c, d, e를 피함으로써 훨씬 더 쉽게 읽을 수 있습니다. 모든 것을 한 줄로 된 구문. – Ervadac

답변

2

그 이유 코드 행 :

++cursor->ptrday; 

당신이 NULL 문자에 도달 할 때까지 C 문자열 배열을 사용하여 구현하고 배열의 이름의 첫 번째 멤버에 대한 포인터에 해당하기 때문에 당신은 ptrday를 증가하는 배열, \0 도달 할 때까지 포인터를 증가시킬 때 \0 전에 모든 문자를 무시합니다.

메모리는 다음과 같이이다 :

char* p = cursor->ptrday; 
*ptrisTrue = 1; 
while (*eday!='\0') { 
    if (*eday != *p) 
     *ptrisTrue=0; 
    ++eday; ++p; 
} 

는 또한 true로 *ptrisTrue를 재설정하는 것을 잊었다 참고 :

_______________ 
    |M|o|n|d|a|y|\0| 
    ________________ 
^Where cursor->ptrday used to and should point to, 
      ^Where cursor->ptrday points to after the second while statement 

당신이 strcmp 기능을 사용하거나이 같은 while 루프를 변경할 수 있습니다이 문제를 해결하기 위해.

+1

하지만 왜 cursor = cursor-> next입니까? 진술이 작동하지 않습니까? – user3011439

0

그러나 왜 cursor = cursor-> next입니까? 진술이 작동하지 않습니까? *headsunday 다음 sunday->next 점을 다시 sunday에 객체이기 때문에,하지만 main()에 할당 head->next=&sunday하여 무한 연결 고리를 만들어 -

것은이 노력하고 있습니다.

방금 ​​head->next=&sunday 줄을 입력하십시오. main(); sunday.next=&monday을 이미 지정했습니다.