Home > front end >  How to store arrays inside array of pointers
How to store arrays inside array of pointers

Time:08-16

i'm trying to implement little program that takes a text and breaks it into lines and sort them in alphabetical order but i encountered a little problem, so i have readlines function which updates an array of pointers called lines, the problem is when i try to printf the first pointer in lines as an array using %s nothing is printed and there is no errors.

I have used strcpy to copy an every single text line(local char array) into a pointer variable and then store that pointer in lines array but it gave me the error.

Here is the code:

#include <stdio.h>

#define MAXLINES 4
#define MAXLENGTH 1000

char *lines[MAXLINES];

void readlines() {
    int i;
    for (i = 0; i < MAXLINES; i  ) {
        char c, line[MAXLENGTH]; 
        int j;
 
        for (j = 0; (c = getchar()) != '\0' && c != '\n' && j < MAXLENGTH; j  ) {
            line[j] = c;
        }

        lines[i] = line;
    }
}

int main(void) {
    readlines();
    printf("%s", lines[0]);
    getchar();

    return 0;
}

CodePudding user response:

One problem is the following line:

lines[i] = line;

In this line, you make lines[i] point to line. However, line is a local char array whose lifetime ends as soon as the current loop iteration ends. Therefore, lines[i] will contain a dangling pointer (i.e. a pointer to an object that is no longer valid) as soon as the loop iteration ends.

For this reason, when you later call

printf("%s", lines[0]);

lines[0] is pointing to an object whose lifetime has ended. Dereferencing such a pointer invokes undefined behavior. Therefore, you cannot rely on getting any meaningful output, and your program may crash.

One way to fix this would be to not make lines an array of pointers, but rather an multidimensional array of char, i.e. an array of strings:

char lines[MAXLINES][MAXLENGTH];

Now you have a proper place for storing the strings, and you no longer need the local array line in the function readlines.

Another issue is that the line

printf("%s", lines[0]);

requires that lines[0] is a string, i.e. an array of characters terminated by a null character. However, you did not put a null character at the end of the string.

After fixing all of the issues mentioned above, your code should look like this:

#include <stdio.h>

#define MAXLINES 4
#define MAXLENGTH 1000

char lines[MAXLINES][MAXLENGTH];

void readlines() {
    int i;
    for (i = 0; i < MAXLINES; i  ) {
        char c; 
        int j;
 
        for (j = 0; (c = getchar()) != '\0' && c != '\n' && j < MAXLENGTH; j  ) {
            lines[i][j] = c;
        }

        //add terminating null character
        lines[i][j] = '\0';
    }
}

int main(void) {
    readlines();
    printf("%s", lines[0]);

    return 0;
}

CodePudding user response:

Try something like this:

#include <stdio.h>
#include <string.h> // for strdup() (if available)

#define MAXLINES 4
#define MAXLENGTH 1000
char *lines[MAXLINES];

void readlines() {
    for( int i = 0; i < MAXLINES; i   ) {
        char c, line[MAXLENGTH   1]; // ALWAYS one extra
        int j = 0; 

        while( (c = getchar()) != '\0' && c != '\n' && j < MAXLENGTH )
            line[ j   ] = c;

        line[j] = '\0'; // <-- Very important

        lines[i] = strdup( line );
    }
}

int main() {
    readlines();

    for( int i = 0; i < MAXLINES; i  ) {
        printf( "%s", lines[i] );
        free( lines[i] );
        lines[i] = NULL; // In case it's used again...
    }

    getchar();
    return 0;
}
  •  Tags:  
  • c
  • Related