Search code examples
cposix

How store each string of getline() inside a (dynamic) array of strings?


I'm using the getline() function to get every line of stdin. Every line is a string with different length:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char *line = NULL;
    size_t foo = 0;
    ssize_t reader;

    while ((reader = getline(&line, &foo, stdin)) != -1) { // %zu of reader is length of line
        printf("%s", line);
    }

    free(line);
    return 0;
}

In every iteration, line is a string and is containing the current line. How can I take each string-line and store it inside an array? There are several things I have tried but none of them worked or they just lead to memory access failure :(


I hope my question is clear? If it's not, please tell me and I will change it!


Solution

  • Unless you know up front how many lines to expect, then you will have to allocate the array dynamically, eg:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        char *line = NULL;
        size_t foo = 0;
        ssize_t reader;
        int result = 0;
    
        int numlines = 0, maxlines = 10;
        char **lines = malloc(sizeof(char*) * maxlines);
    
        if (!lines) {
            printf("error allocating array\n");
        }
        else {
            while ((reader = getline(&line, &foo, stdin)) != -1) { // %zu of reader is length of line
                printf("%s", line);
    
                if (numlines == maxlines) {
                    maxlines *= 2; // <-- or use whatever threshold makes sense for you
                    char **newlines = realloc(lines, sizeof(char*) * maxlines);
                    if (!newlines) {
                        printf("error reallocating array\n");
                        result = -1;
                        break;
                    }
                    lines = newlines;
                }
    
                lines[numlines] = line;
                ++numlines;
    
                line = NULL;
                foo = 0;
            }
            free(line); // <-- in case getline() or realloc() failed...
    
            // use lines up to numlines as needed
    
            // free lines
            for(int i = 0; i < numlines; ++i) {
                free(lines[i]);
            }
            free(lines);
        }
    
        return result;
    }