I am doing an exercise for fun from KandR C programming book. The program is for finding the longest line from a set of lines entered by the user and then prints it.
Her
realloc
call will re-allocate memory by taking a pointer to a storage area on the heap
i.e a dynamically allocated memory result of a call to malloc
.
In your case the problem is that you are allocating memory on the stack
and not dynamically by a call to malloc
which results memory allocation on the heap
. And, passing the pointer of the automatic
character array line
to _getline
which uses it for call to realloc
. So, you got the error.
Try dynamically allocating the character array line
:
char* line = (char *) malloc(MAXLINE);
char* longest = ( char *) malloc(MAXLINE);