【发布时间】:2016-03-17 15:59:20
【问题描述】:
我将阅读几行文本,所以我想将它保存在一个全局变量中。我做了这样的事情:
#include <stdbool.h>
#include <stdio.h>
static char currentLine[MAX_LINE_LENGTH];
static bool readNextLine(void)
{
return getline(¤tLine, NULL, stdin);
}
我得到的错误是
cc -Wno-unused-function -Wno-unused-variable -O2 -MD -MP -Wall -Werror -c src/parse.c -o build/parse.o
src/parse.c: In function ‘readNextLine’:
src/parse.c:14:20: error: passing argument 1 of ‘getline’ from incompatible pointer type [-Werror=incompatible-pointer-types]
return getline(¤tLine, NULL, stdin);
^
In file included from src/parse.c:2:0:
/usr/include/stdio.h:678:20: note: expected ‘char ** restrict’ but argument is of type ‘char (*)[100000]’
extern _IO_ssize_t getline (char **__restrict __lineptr,
我做错了什么?
【问题讨论】:
-
man getline:
If *lineptr is NULL, then getline() will allocate a buffer for storing the line, which should be freed by the user program. (In this case, the value in *n is ignored.) Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc(3)-allocated buffer *n bytes in size.TL;DR: 你不能那样做。 -
将错误的类型传递给
getline,这就是您收到错误消息的原因。指向指针和指向数组的指针都是不同的。