2017-10-19 29 views
0

저는 C 언어를 처음 사용하며 사용자 입력이있는 동안 몇 줄의 코드를 실행하려고합니다.C 프로그램 - 디 레퍼런스 포인터

char names[SIZE][LENGTH];  
while(fgets(names, LENGTH, stdin) != '\0') 

오류는 다음과 같습니다 : 어떤 이유로, 나는이 줄에서 오류를 얻고있다.이 라인에서 "다중 마커 비교 포인터 제로 문자 상수 사이에 당신이 포인터 역 참조로 의미했다 인수 한을 전달.? 호환되지 않는 포인터 유형에서 '는 fgets'의.

어떤 아이디어?

+1

좋은 C 책이 필요합니다. –

답변

2

당신이 2 차원 배열의 요소로 라인을 읽어가는 것 같다.는 C 표준에서

(7.21.7.2를 fgets 함수)

3 The fgets function returns s if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned.

따라서 올바른 루프는

size_t i = 0; 
while(i < SIZE && fgets(names[i], LENGTH, stdin) != NULL) 
{ 
    //... 
    ++i; 
} 

처럼 보일 수 있습니다 또는 당신은 빈 줄이 다음 encounterd 때 라인 읽기를 중단하려는 경우 당신은

size_t i = 0; 
while(i < SIZE && fgets(names[i], LENGTH, stdin) != NULL && names[i][0] != '\n') 
{ 
    //... 
    ++i; 
} 

오류 메시지를 쓸 수 있습니다 컴파일러에서 발행 한 내용은 다음과 같습니다.

Passing argument 1 of 'fgets' from incompatible pointer type.

첫 번째 인수로서 사용이 함수 호출

fgets(names, LENGTH, stdin) 

names210

char (*)[LENGTH] 입력 갖지만 함수 타입 char * 의 인수를 기대한다.

"Comparison between pointer and zero character constant. Did you mean to dereference the pointer?

이 메시지는 함수 포인터에 의해 반환 된이 널 포인터와 비교하거나 문자 '\0'으로 반환 된 포인터에 의해 캐릭터가 지적 비교 거라고 여부를 컴파일러가 결론을 내릴 수 없음을 의미합니다.

+0

NULL을 사용해 보았습니다. 그러나 호환되지 않는 포인터 유형에서 'fgets'인수 1을 전달하면 오류가 발생합니다. – Liz