C 读取未知维矩阵

C 读取未知维矩阵,c,C,我正试图从这样格式化的文件中读取矩阵 1 2 3 4 4 5 6 7 8 9 10 11 * 1 0 1 0 1 1 1 1 1] 1 2 3 4 4 5 6 7 8 9 10 11 * 1 0 1 0 1 1 1 1 1] 但我不知道如何将多维数组及其维数由rows[]和cols[]数组给出的值传递给函数read_matrix() 我能得到两个矩阵的正确维数 #include <stdio.h> #include <string.h> #include <stdl

我正试图从这样格式化的文件中读取矩阵

1 2 3 4 4 5 6 7 8 9 10 11 * 1 0 1 0 1 1 1 1 1] 1 2 3 4 4 5 6 7 8 9 10 11 * 1 0 1 0 1 1 1 1 1] 但我不知道如何将多维数组及其维数由rows[]和cols[]数组给出的值传递给函数read_matrix()

我能得到两个矩阵的正确维数

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

int get_dim(char *file, int *r, int *col);
/*gets the dimension and type of operation*/

void read_matrix(char *file, int (*matA)[], int(*matB)[], int *m, int *n);

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

    int i,j;
    int rows[2]= {0,1}; /* The last line of the file does not contain '\n' */
    int cols[2]= {0,0};
    int operation; /*type of operation 1 for * and 2 for + */

    operation = get_dim(argv[1], rows, cols);

    int A[rows[0]][cols[0]];
    int B[rows[1]][cols[1]];

    printf("A is a matrix %d x %d\n",rows[0], cols[0]);

    if(operation != 0)
        printf("B is a matrix %d x %d\n", rows[1], cols[1]);

    read_matrix(argv[1], A, B, rows, cols);

    /*printing the matrices */

    for(i = 0; i < rows[0]; i++){
        for(j=0; j < cols[0]; j++){
            printf("%d ", A[i][j]);
        }
        printf("\n");
    }

    printf("*\n");

     for(i = 0; i < rows[1]; i++){
        for(j=0; j< cols[1]; j++){
            printf("%2d ", B[i][j]);
        }
        printf("\n");
    }    
    return 0;
}

int get_dim(char *file, int *r, int *col){

   FILE *fp; 
   int c;
   int op=0;
   int n =0; /*to check all coefficients in a row */

   /* opening file for reading */
   fp = fopen(file, "r");

   if(fp == NULL) {
      perror("Error opening file");
   }

   while ( (c = getc(fp)) != ']')
   {       
       if(isdigit(c) && n==0){
           if(op == 0)
               col[0]++;
           else
               col[1]++;
       }        

    //Count whenever new line is encountered
    if (c == '\n'){
        n=1;
        if(op == 0)
            r[0]++;
        else 
            r[1]++;
    }     

        if(c == '*'){
            op=1;
            n =0;
            c = getc(fp);
        }
        else if(c == '+'){
            op=2;
            c = getc(fp);
        }

        //take next character from file.
    }
    fclose(fp);
    return op;
}

void read_matrix(char *file, int (*matA)[], int(*matB)[], int *m, int *n){

    int i,j;
    FILE *fp;

    if( (fp = fopen(file, "r")) != NULL ){

        for(i=0; i < m[0]; i++)
            for(j=0; j < n[0]; j++)
                fscanf(fp, "%d", &matA[i][j]);    

        /*skip the line containing the character operator */
        while ( fgetc(fp) != '\n')
            fgetc(fp);        

        for(i=0; i < m[1]; i++)
            for(j=0; j < n[1]; j++)
                fscanf(fp, "%d", &matB[i][j]);    
    }

    fclose(fp);
}
#包括
#包括
#包括
#包括
int get_dim(字符*文件,int*r,int*col);
/*获取操作的维度和类型*/
无效读取矩阵(字符*文件,int(*matA)[],int(*matB)[],int*m,int*n);
int main(int argc,char*argv[]){
int i,j;
int rows[2]={0,1};/*文件的最后一行不包含“\n”*/
int cols[2]={0,0};
int运算;/*运算类型1表示*和2表示+*/
操作=获取尺寸(argv[1],行,列);
int A[rows[0]][cols[0]];
int B[rows[1]][cols[1]];
printf(“A是一个矩阵%d x%d\n”,行[0],列[0]);
如果(操作!=0)
printf(“B是一个矩阵%d x%d\n”,行[1],列[1]);
读取矩阵(argv[1],A,B,行,列);
/*打印矩阵*/
对于(i=0;i<行[0];i++){
对于(j=0;j
我应该这样定义指针:int(*matA)[cols[0]]和int(*matB)[cols[1]]。
在read_matrix()中,通过声明这个指针:int(*matB)[]我得到了一个错误:数组的使用无效,边界未指定,我理解。但是边界是由get_dim()函数确定的。

get_dim函数的结果是在运行时确定的,像matA这样的静态数组的大小必须在编译时确定。以以下代码为例:

volatile size_t length = 5;
int myArray[length];
或者另一个例子:

size_t length;
//take input from the user...
int myArray[length]
这些示例无法编译,因为长度只能在运行时确定


你必须使用动态分配的数组来完成你想做的事情。

volatile
在这里似乎真的不合适。我理解这个问题,但我不知道如何解决它注意,因为问题被标记为
C
C99
的其中一个功能确实是可变长度数组,上面的例子完全有效。在另一个答案中,数组没有被记住,我错了,重复的也错了,对不起,我不知道你的问题是否会被重新打开,但我编辑了我的答案并取消了删除,您可以指定计算单元格所在位置所需的列数,我在回答中给出了一个示例