Passing a matrix in a function (C)

前端 未结 5 857
梦毁少年i
梦毁少年i 2020-11-30 11:49

I have an issue passing a matrix to a function in C. There is the function I want to create:

void ins (int *matrix, int row, int column);

b

5条回答
  •  我在风中等你
    2020-11-30 12:22

    You need to pass a pointer with as much levels of indirection (*) as the number of dimensions of your matrix.

    For example, if your matrix is 2D (e.g. 10 by 100), then:

    void ins (int **matrix, int row, int column);
    

    If you have a fixed dimension (e.g. 100), you can also do:

    void ins (int (*matrix)[100], int row, int column);
    

    or in your case:

    void ins (int (*matrix)[SIZE], int row, int column);
    

    If both your dimensions are fixed:

    void ins (int matrix[10][100], int row, int column);
    

    or in your case:

    void ins (int matrix[SIZE][SIZE], int row, int column);
    

提交回复
热议问题