나는 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;), 구조의 날 구성원에 대한 커서 값은 "월요일"대신 두 개의 따옴표 ("")로 설정됩니다. 이 문제를 어떻게 해결할 수 있습니까?
먼저, 첫 번째 불일치에서'isTrue'를 0으로 설정하고 코드의 다른 곳에서는 결코 0이 아닌 값을 설정하지 마십시오. –
'bool' 변수는 일반적으로'false' ('0'보다 우선) 또는'true' ('1'보다 우선)로 지정되어야합니다. ''트루 (Truje) '라는 이름은 조금이라도 마음을 불허합니다. 무슨 말이 사실입니까? 그리고 '거짓말 쟁이'는 여전히 마음이 굽혀 있습니다. 때때로 isFalse에서 repoint 할 필요가 있습니까? 그것은 명명법을 걱정하고있다. –
나는 Jonathan에 동의한다. 나는 단지 미래의 이슈로 이어질 수있는 글로벌 변수를 정의하는 것을 추가 할 것이고, 글로벌하게 그것들을 "모든"프로그램으로 설정하는 것은별로 의미가 없다. 게다가, 솔직히 말하면, 여러분의 코드는 a와 b, c, d, e를 피함으로써 훨씬 더 쉽게 읽을 수 있습니다. 모든 것을 한 줄로 된 구문. – Ervadac