As already has been mentioned, you need to allocate space for pointers, not chars:
char **array;
array = malloc(bufsize * sizeof(char*));
Also you need to allocate space for separate lines, and copy lines to it:
while ((getline(&line, &bufsize, fp)) != -1) {
printf("%s", line);
array[i] = malloc(strlen(line) + 1);
strcpy(array[i], line);
i++;
}
A mistake in your code is that all array[i]
points to the same string in variable line
, which is refilled by getline()
every loop cycle.
It may be useful to read manual about getline. It permits allocation of strings by itself, but don't forget to use free().
array[0] = NULL;
while ((getline(&array[i], &bufsize, fp)) != -1) {
printf("%s", array[i]);
array[i + 1] = NULL;
i++;
}