#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char* buffer = malloc(100 * sizeof(char));
size_t n = 3;
getline(&buffer, &n, stdin);
printf("%s\n", buffer);
free(buffer);
}
I thought the second parameter in getline
, size_t *n
, was to limit the number of characters read. But when I tried with larger input, it still read all the input. I searched in the man pages and online but could not find an answer. Could anyone explain it for me?
From
getline
man pages:Given
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
Emphasis mine. In short,
n
is updated to make sure the line fits.