int bytes_read;
int nbytes = 100;
char *Name;
Name = (char *) malloc (nbytes + 1);
bytes_read = getline (&Name, &nbytes, stdin);
/* Warning Message when compiled
warning: incompatible pointer types passing 'int *' to
parameter of type 'size_t *' (aka 'unsigned long *')
[-Wincompatible-pointer-types]
...bytes_read = getline (&Name, &nbytes, stdin);
^~~~~~~
*/
我试图在C中使用getline()函数。编译时运行良好,但有一个警告。
为什么会有警告?我想不出来。
最佳答案
您传递给的&nbytes
的第二个参数getline
属于int *
类型,但getline
需要size_t *
类型的第二个参数。
更改声明
int nbytes = 100;
到
size_t nbytes = 100;
或者你可以把第二个论点
bytes_read = getline (&Name, (size_t *)&nbytes, stdin);
(对于
int nbytes = 100;
,从不将&nbytes
转换为size_t *
)关于c - 在C中使用getline()的警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20250511/