Hello i'm practising C and i have a little problem with the following code. First of all, my program just reads the input from the user and if there is memory availble, it stores it, otherwise it makes nothing.
I have an array of pointers to char called "lines" and an array of chars for the temporary storage of the input called "line".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWIDTH 81
#define MAXLINES 100
int main ()
{
char* lines [MAXLINES];
char line[MAXWIDTH];
int i ;
int n ;
Then i will check if my array of pointers has space and the input is non-zero. I do it in a for-loop to fill the array and normally the for-loop should stop when i type in nothing and just press enter or when the array is full. If there is space i will check if there is enough memory in the space where the pointer is pointing to. If that's ok (!= NULL), the program copies the input from gets(line) in the memory.
for (n = 0; n < MAXLINES && gets(line) != NULL; n++)
{
if ((lines[n] = malloc(strlen(line) + 1)) == NULL)
exit (1);
strcpy(lines[n], line);
}
The rest of the code is just for the output and freeing of the memory.
for (i = 0; i < n; i++)
{
puts(lines[n-i-1]);
free(lines[n-i-1]);
}
return 0;
}
Now the problemis , that the program runs without any errors, but it's not working as i want. It is just performing a infinte loop where i can type in as long as i want, what i want without any reaction.