2017-10-19 82 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