Storing lines in an array
I don't know what _getline is; perhaps it's something like the file-input function described here.



This looks strange ...

int nlines;

nlines = 0;


I think that C now (like C++) allows you to delay defining a local variable until you initialize it. So there's no need to declare your variables at the top of the function.



Passing MAXLEN is probably wrong: because if (MAX_LEN &gt; storage_size) then you give _getline the opportunity to write past the end of the buffer.

Allowing a buffer overrun is a bug.

Instead you could do something like:

int n_lines = 0;

for (;;)
{
    // define local buffer inside function
    char buffer[MAX_LEN];
    int line_length = _getline(buffer, MAXLEN);
    if (line_length == 0)
        break;
    if (n_lines &gt;= maxlines)
        return -1;
    if (line_length &gt; storage_size)
        return -1;
    buffer[line_length - 1] = '\0';
    // copy local buffer to passed-in buffer
    strcpy(storage, buffer);
    lineptr[n_lines] = storage;
    storage += line_length;
    storage_size -= line_length;
    ++n_lines;
}

return n_lines;


Or you can read directly into the passed-in buffer, to avoid strcpy and avoid defining MAXLEN:

int line_length = _getline(buffer, storage_size);
if (line_length == storage_size)
{
    // fail unless there's nothing more to read
    char temp[1];
    if (0 != _getline(temp, 1))
        return -1;
}




I'm not sure what this statement does ...

storage[len - 1] = '\0';


... if the end-of-line marker is a two-character "\r\n" sequence (which it is on Windows).



You could possibly edit your variable names; if you're going to use_underscores as you did for storage_size, then perhaps:


maxlines -> max_lines
len -> line_length
lineptr -> lines or lines_array
nlines -> num_lines or line_number or line_counter


The least obvious variable name was len.