Passing a matrix in a function (C)

前端 未结 5 858
梦毁少年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:06

    If you have a modern C compiler you can do the following for 2D matrices of any sizes

    void ins (size_t rows, size_t columns, int matrix[rows][columns]);
    

    Important is that the sizes come before the matrix, such that they are known, there.

    Inside your function you then can access the elements easily as matrix[i][j] and the compiler is doing all the index calculations for you.

提交回复
热议问题