如何在 C 语言中使用指针和函数创建一个二维数组并显示出来?

6

我正在尝试编写两个函数,一个用于读取矩阵(2D数组),另一个用于将其打印出来。到目前为止,我有:

    /* Read a matrix: allocate space, read elements, return pointer. The
       number of rows and columns are given by the two arguments. */
    double **read_matrix(int rows, int cols){

        double **mat = (double **) malloc(sizeof(double *)*rows);
        int i=0;
        for(i=0; i<rows; i++){
        /* Allocate array, store pointer  */
        mat[i] = (double *) malloc(sizeof(double)*cols); 
       //what to do after??

         return mat;
    }

然后是打印矩阵的函数,不确定是否正确。
void print_matrix(int rows, int cols, double **mat){
    for(i=0; i<rows; i++){    /* Iterate of each row */
      for(j=0; j<cols; j++){  /* In each row, go over each col element  */
      printf("%f ",mat[i][j]); /* Print each row element */
  }
}
}

以下是我使用的主函数:

    #include <stdlib.h>


    double **read_matrix(int rows, int cols);
    void print_matrix(int rows, int cols, double **mat);
    void free_matrix(int rows, double **mat);

    int main(){

      double **matrix;
      int rows, cols;
      /* First matrix */
      printf("Matrix 1\n");
      printf("Enter # of rows and cols: ");
      scanf("%d %d",&rows,&cols);
      printf("Matrix, enter %d reals: ",rows*cols);
      matrix = read_matrix(rows,cols); 
      printf("Your Matrix\n");  /* Print the entered data */
      print_matrix(rows,cols,matrix);
      free_matrix(rows, matrix);   /* Free the matrix */

        return 0;
}

2
投票关闭,因为它看起来更像是一个适合于代码审查StackExchange而不是StackOverflow的问题。虽然代码基本上看起来没问题。 - Ben Jackson
你的确切问题是什么?我在这里看不到任何问题... - Christoph
@BenJackson:它被关闭了,因为在review.stackexchange上是离题讨论。这不是一个好的体验。 - jfs
1个回答

7

试试这个。可能对你有帮助。

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

double **read_matrix(int rows, int cols);
void print_matrix(int rows, int cols, double **mat);
void free_matrix(int rows, double **mat);

double **read_matrix(int rows, int cols){

    double **mat = (double **) malloc(sizeof(double *)*rows);
    int i=0,j=0;
    for(i=0; i<rows; i++)
    /* Allocate array, store pointer  */
        mat[i] = (double *) malloc(sizeof(double)*cols); 

       for(i=0; i<rows; i++){
           for(j=0; j<cols; j++){
               scanf("%lf",&mat[i][j]);
           }
       }
     return mat;
}

void print_matrix(int rows, int cols, double **mat){
    int i=0,j=0;
  for(i=0; i<rows; i++){    /* Iterate of each row */
        for(j=0; j<cols; j++){  /* In each row, go over each col element  */
            printf("%lf ",mat[i][j]); /* Print each row element */
        }
        printf("\n");
    }
}

void free_matrix(int rows, double **mat){
    int i=0;
    for(i=0;i<rows;i++)    
        free(mat[i]);
    free(mat);
}

int main(){

  double **matrix;
  int rows, cols;
  /* First matrix */
  printf("Matrix 1\n");
  printf("Enter # of rows and cols: ");
  scanf("%d%d",&rows,&cols);
  printf("Matrix, enter %d reals: \n",rows*cols);
  matrix = read_matrix(rows,cols); 
  printf("Your Matrix\n");  /* Print the entered data */
  print_matrix(rows,cols,matrix);
  free_matrix(rows, matrix);   /* Free the matrix */

  return 0;
 }

执行:

:~$ gcc  exam.c
:~$ ./a.out 
Matrix 1
Enter # of rows and cols: 3
4
Matrix, enter 12 reals: 
1
2
3
4
5
6
7
8
9
9
0
1
Your Matrix
1.000000 2.000000 3.000000 4.000000 
5.000000 6.000000 7.000000 8.000000 
9.000000 9.000000 0.000000 1.000000 

@Jenny,这段代码没有段错误。我已经修改了步骤。 - Grijesh Chauhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接