2

I am trying to read a Multidimensional String Matrix from a file and store it to a array of Character Arrays like this:

char *A[M][N];

This works fine if I stay inside the main function. But when i am trying to call a function by reference it doesn't work.

Function Call:

readMatrix(file,A);

Function Header:

int readMatrix(FILE *file,char *matrix[][]);

I also tried this:

int readMatrix(FILE *file,char ***matrix);

which also doesn't work. I want to manipulate the Array in the function therefore i need to make a reference call.

11
  • 4
    You need to add code that populates your array Commented Jan 2, 2017 at 17:43
  • 2
    try int readMatrix(FILE *file, size_t m, size_t n, char *matrix[m][n]); and call readMatrix(file, M, N, A); Commented Jan 2, 2017 at 17:43
  • 1
    What won't work exactly? Please give more details and paste the readMatrix function's code Commented Jan 2, 2017 at 17:45
  • 3
    a 2D matrix of char* is not the same as a char***. your compiler shouldn't accept any of this. Commented Jan 2, 2017 at 17:51
  • 1
    int readMatrix(FILE *file, size_t m, size_t n, char *matrix[m][n]); readMatrix(file, M, N, A); this works very fine , thanks a lot ;-) Commented Jan 2, 2017 at 17:55

2 Answers 2

1

You must pass N as part of the matrix type to your ReadMatrix function, and since it is not known at compile time, you must pass these as arguments too:

int readMatrix(FILE *file, size_t M, size_t N, char *matrix[][N]);

You could indeed also specify the array argument as char *matrix[M][N], but the first dimension size M is ignored for a function argument as it only receives a pointer to the array.

Here is an example:

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

int readMatrix(FILE *file, size_t rows, size_t cols, char *matrix[][cols]) {
    int rc = 0;
    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < cols; j++) {
            char buf[80];
            if (rc == 0 && fscanf(file, "%79s", buf) == 1) {
                matrix[i][j] = strdup(buf);
            } else {
                matrix[i][j] = NULL;
                rc = -1;
            }
        }
    }
    return rc;
}

int main(void) {
    size_t rows, cols;
    if (scanf("%zu %zu", &rows, &cols) != 2)
        return 1;
    char *mat[rows][cols];
    if (readMatrix(stdin, rows, cols, mat) == 0) {
        for (size_t i = 0; i < rows; i++) {
            for (size_t j = 0; j < cols; j++) {
                printf(" %8s", mat[i][j]);
            }
            printf("\n");
        }
    }
    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < cols; j++) {
            free(mat[i][j]);
        }
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

7 Comments

There will be no problem for size_t N ? Will it not be replaced by 100 ?
you can omit is most left dim. It is not right dim.
the size is always the first line of the file. the readMatrix method is called after the first line is read
@informaticienzero: correct! I updated the code with a working example. Just redirect a dictionary into the program's standard input.
@BLUEPIXY: I missed the important info provided in the comments. Fixed
|
0

You can do it by using this approach:

#include <stdio.h>
#define MAX_WORD_SIZE 10
void readMatrix(FILE*,char****);
int main(void){
    FILE* cin = fopen("input.txt","r");
    char*** inputMatrix;
    readMatrix(cin,&inputMatrix);
    for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                printf("%s ",*(*(inputMatrix+i)+j));
            }
            printf("\n");
    }
    fclose(cin);
    return 0;
}
void readMatrix(FILE* cin,char**** matrix){
    int rowNum,columnNum;
    char buff[10];
    int intBuff;
    fscanf(cin,"%d",&intBuff);
    rowNum = intBuff;
    fscanf(cin,"%d",&intBuff);
    columnNum = intBuff;
    *matrix = malloc(rowNum*sizeof(char**));
    for(int i=0;i<rowNum;i++){
        *(*matrix+i) = malloc(columnNum*sizeof(char*));
    }

    for(int i=0;i<rowNum;i++){
        for(int j=0;j<columnNum;j++){
            *(*(*matrix+i)+j) = malloc(MAX_WORD_SIZE*sizeof(char));
            fscanf(cin,"%s",*(*(*matrix+i)+j));
        }
    }
}

with this input file(input.txt file within same folder), this code is working well on Linux GCC

3 3
ab cd ef
gh ij kl
mn op qr

In the first line of input.txt file, first integer denotes row number, second integer denotes column number.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.