I use getline
function to read a line from STDIN
.
The prototype of getline
is:
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
I use this as a test program which get from 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;
}
This works fine.
My doubts are?
Why use
char **lineptr
insteadchar *lineptr
as a parameter of functiongetline
?Why it is wrong when I use the following code:
char **my_string; bytes_read = getline(my_string, &nbytes, stdin);
I am confused with
*
and&
.
Here is part of warnings:
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 *’
I use GCC version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5).