3

This might be one of the common questions, but all solutions I've seen so far are not working. I want to dynamically allocate 2D array of chars. I get these chars from a .txt file. I even have set number of rows (int r) and columns (int s). Allocation itself is working but whenever I try to load chars from a file into this array, it crashes. Have no idea why.

File is ordered in this way:

Here is the code:

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

int main(int argc, char* argv[])
{

Opening desired file:

    FILE* file;
    file = fopen(argv[1], "r");
    if (file == NULL) {
        printf("Error occurred when loading the file, program quits now.");
        return 1;
    } else {
    printf("File loaded successfully.");
    }

Getting info about number of columns and rows (r are rows, s are columns):

    int r,s,i,j;
    char arrayInfo[6];
    fgets (arrayInfo, 6, file);
    char* comma = strchr(arrayInfo, ',');
    s = atoi(comma - 1)+1;
    r = atoi(comma + 1);

Memory allocation for a 2D array of chars:

    char **array = malloc(r * sizeof(char *));
    for(i=0;i<r;i++){
        array[i] = malloc(s * sizeof(char));
    }

NOT WORKING Loading of chars from a file and then printing them. This code would work with "char array [r][s];" instead of dynamic allocation.

    for (j=0;j<r;j++) {
        for (i=0;i<s;i++) {
            array[i][j] = fgetc (file);
        }
    }

    for (j=0;j<r;j++) {
        for (i=0;i<s;i++) {
            printf ("%c",array[i][j]);
        }
    }
    return 0;
}
7
  • 2
    Try switching your indices: use array[j][i] instead of array[i][j].
    – Frxstrem
    Dec 16, 2014 at 18:30
  • Also, you should always verify that malloc doesn't return NULL (as it does if, for instance, you run out of memory). Unless you have large values of r and s, this probably won't matter, but it's good programming practice nonetheless.
    – Frxstrem
    Dec 16, 2014 at 18:33
  • Your code would become much simpler if you'd read the file line-by-line using the existing getline library function.
    – 5gon12eder
    Dec 16, 2014 at 18:37
  • All ints in the snippets should really be size_ts. No need to negative indices.
    – alk
    Dec 16, 2014 at 19:21
  • Also sizeof(char) is redundant as sizeof(char) is definded to be equal 1.
    – alk
    Dec 16, 2014 at 19:22

2 Answers 2

2

If there is a crash I doubt that issue is with:

array[i] = malloc(s * sizeof(char));

Please make sure whether malloc() succeeded or not first, later try to write to this allocated memory.

a[i][j] i is your row and j is your column. We see in your code it is interchanged.

I don't know how your file looks like but please re-check the below evaluation

s = atoi(comma - 1)+1; /* comma is a pointer and you are decrementing it by 1? */

, is being used in strchr and later the pointer is decremented by 1 to get the integer value which is not what you want. Fix this also.

0

Thank you for your input guys - array [i][j] needs to be changed to array[j][i], then it works as it should.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.