我使用getline
函数从STDIN
读取一行。
getline
的原型为:
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
我使用这个作为测试程序,它是从http://www.crasseux.com/books/ctutorial/getline.html#getline获取的。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int atgc, char *argv[])
{
int bytes_read = 1;
int nbytes = 10;
char *my_string;
my_string = (char *)malloc(nbytes+1);
puts("Please enter a line of text");
bytes_read = getline(&my_string, &nbytes, stdin);
if (bytes_read == -1)
{
puts ("ERROR!");
}
else
{
puts ("You typed:");
puts (my_string);
}
return 0;
}
这个很好用。
我的疑问是什么?
为什么在函数
getline
的参数中使用char **lineptr
而不是char *lineptr
?为什么当我使用以下代码时会出错:
char **my_string; bytes_read = getline(my_string, &nbytes, stdin);
我对
*
和&
感到困惑。
以下是部分警告信息:
testGetline.c: In function ‘main’:
testGetline.c:34: warning: pointer targets in passing argument 2 of
‘getline’ differ in signedness
/usr/include/stdio.h:671:
note: expected ‘size_t * __restrict__’ but argument is of type ‘int *’
testGetline.c:40: warning: passing argument 1 of ‘putchar’ makes integer
from pointer without a cast
/usr/include/stdio.h:582: note: expected ‘int’ but argument is of
type ‘char *’
我使用的是GCC版本4.4.5(Ubuntu/Linaro 4.4.4-14ubuntu5)。
bytes_read
时打错了字。还有,“incertitude” 是什么意思? - Lightness Races in Orbit