2017-10-19 81 views
0

我是新的C,我試圖運行的幾行代碼,而沒有用戶輸入。C程序 - 取消引用指針

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

的錯誤是::出於某種原因,我在這條線得到一個錯誤。「多個標記在這條線比較指針和零字符常量之間您的意思是取消引用指針傳遞參數1。?從兼容的指針類型「與fgets」。

任何想法?

+1

你需要一個好的C書 –

回答

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) 

表達names用作第一參數的類型爲char (*)[LENGTH]但功能期望類型char * 的參數。

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

此消息意味着,無論是由函數返回的指針與一個空指針比較,或者你正想通過返回的指針與字符'\0'比較字符指出編譯器不能斷定。

+0

我嘗試使用NULL,但是,我得到的錯誤:從兼容的指針類型過客「與fgets」的參數1 – Liz