It would help if you described how it doesn't work - what is the difference between what it does and what you expect it to do?
Here is one guess of what may be going wrong:
What happens when you have a newline on the line in red?Code:if(i==0) { /* allocate space for the string being stored */ *array[line]=malloc(sizeof(char)*size); } /* if we are at the end of the line, then we prepare to store * the next line of input in the next string slot. */ if(c == "\n") { line++; *(array+i) = "\0"; /* terminate string at end of line */ i=0; } /* store the character in its correct position in the char array */ *(array+i) = c;
As to how to allocate the right amount of memory for each line:
1. I wouldn't worry about it in this case - just make it as long as it needs to be.
2. You can do that in different ways - in a PC or such machine (that is, not in a small embedded system) I would make a ridiculously large buffer (e.g 1000 or 10000 characters) on the stack [local variable] that you read your line into, then when you find the newline, allocate enough memory to hold the string.
In a case where using large local variables is unsuitable [embedded systems for example], I'd use either of these:
a) start with a small buffer (16-32 bytes) allocated with malloc, then use realloc to grow it by a factor of 2x each time it is full.
b) or allocate a large enough buffer [as above, 1000-10000 chars], and then use the method described when using a large local buffer. Free the temporary buffer at end of function.
--
Mats