C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array.

If i do this:
using a char **list to store strings
allocate memory to it

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


int main(int argc, char *argv[])
{
    char *prog = argv[0];
    
    char **linelist;
    int listlen = 3;
    int index = 0;

    char *line;
    int linelen = 80;
    int len = 0; 


    /*allocate memory*/
    /*****************/
    linelist = calloc(sizeof(char **), listlen);
    if (linelist == NULL) {
        fprintf(stderr, "%s: Error. Could not allocate memory\n", \
                prog);
        exit(42);
    }

    line = malloc(linelen);
    if (line == NULL) {
        fprintf(stderr, "%s: Error. Could not allocate memory\n", \
                prog);
    }

    /*fill it manually*/
    /******************/
    linelist[0] = "foo";
    linelist[1] = "bar";
    linelist[2] = "baz";


    /*print result on stdout, dummy*/
    /*******************************/
    for (index = 0; index < 3; ++index) {
        printf("%s\n", linelist[index]); 
    }

    
    return 0; 
}

it works. As far i see.

If i do this:
using a char **list
allocate memory to it
using getline to get input from stdin
allocate memory to each line:

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

int main(int argc, char *argv[])
{
    char *prog = argv[0]; 
    
    char **linelist;
    int listlen = 3; 
    int index = 0; 

    char *line; 
    int linelen = 80; 
    int len = 0; 

    

    /*allocate memory*/
    /*****************/
    linelist = calloc(sizeof(char **), listlen); 
    if (linelist == NULL) {
        fprintf(stderr, "%s: Error. Could not allocate memory\n", \
                prog); 
        exit(42); 
    }

    line = malloc(linelen); 
    if (line == NULL) {
        fprintf(stderr, "%s: Error. Could not allocate memory\n", \
                prog); 
    }

    /*read input with getline*/
    /*************************/
    /* the former while  fails, lets check if the latter succeeds: well, kinda*/
    /*while (len = getline(&line, &linelen, stdin) && index < linelen -1 )   ) {*/
    while (index < listlen && (len = getline(&line, &linelen, stdin))   ) {
        /* debugging purpose*/    
        printf("size of line is: %d\n", len); 
        
        linelist[index] = malloc(len); 
        if (linelist[index] == NULL) {
            fprintf(stderr, "%s: Error. Could not allocate memory\n", \
                    prog); 
        }

        linelist[index] = line;
        /*debugging purpose*/
        printf("line is: %s\n", linelist[index]); 
        /*gives correct output*/    
        ++index; 
    }

    /*result*/
    /********/
    /* both  gives wrong output,all give the last string entered on stdin*/
    for (index = 0; index < listlen -1; index++) {
        printf("element %d of linelist is: %s\n",  \
            index, linelist[index]); 
        }

    printf("%s\n", linelist[0]); 
    printf("%s\n", linelist[1]); 
    printf("%s\n", linelist[2]); 

    return 0; 
}

It fails.
What seems to get stored in the **list is three times the last input i get from stdin.
I don't understand why.

Here is the way i get the stdin as a standalone:

    while (index < listlen && (len = getline(&line, &linelen, stdin))   ) {
        /* debugging purpose*/    
        printf("size of line is: %d\n", len); 
        
        linelist[index] = malloc(len); 
        if (linelist[index] == NULL) {
            fprintf(stderr, "%s: Error. Could not allocate memory\n", \
                    prog); 
        }

        linelist[index] = line;
        /*debugging purpose*/
        printf("line is: %s\n", linelist[index]); 
        /*gives correct output*/    
        ++index; 
    }

printf("line is. %s\n", linelist[index]
gives the right string (the actual stdin). But it doesn't seem to get stored (correct) in "char **linelist".

thanks for a hint.

linelist[index] = line;

line here is a pointer to something returned earlier. so you've set each element to point to the same place. you malloc enough for the string. you'd want to copy it with strcpy, or use strdup

strcpy(linelist[index],line);
1 Like

Thanks. With

strcpy(linelist[index], line);

it works. But i think i had to add +1 to:

linelist[index] = malloc(len+1); 

[/FONT]

I still fail to understand why.
To me it looks like the while loop reads one line at a time,
allocates memory to linelist[index],
then sets linelist[index] to be the line read with getline,
and increments index +1,
so that during the next run of the loop the next element of linelist will be set.
(Obviously it doesn't, but i can't see why).

you merely set it to point to that same address each time. C has char type. a string is pointer (char *), so the variable itself is only memory location of the start. the end is when you hit a \0. so when you do line = malloc() you grab enough space for 79 chars and the null. getline writes to it, but it doesn't change the address.. that address stays the same here.

setting a pointer to a pointer only copies the address. you need to copy what's at the address. this requires a loop, continuing either a set length like with memcpy, or until null byte, strcpy.

pointers are sometimes a tough concept to fully grasp

edit: yes, the +1 to the malloc would be so you have room to store a \0 which is needed to terminate strings.

Ok, thanks. I know in what direction to look.

I also found this and had some ideas (but like you say: it really can be hard to understand)

Using *retptr = p; in allocstr and &copystr in the caller, which showed me that i don't fully understand pointers yet.

  • means 'use an address as its contents', i.e. * for a type of char * would get you its first element, * for a type of int * would get you an integer, etc, etc.

& means 'turn a variable into an address'. & for a type of int would get you an int * , and so forth.

If you're still confused you'll have to start posting specific questions, because there's lots of stuff on that page.