开发者

print matrix of any size

I have this method开发者_如何学运维 for prinitng:

void printMatrix(int matrix[][3]){
  int i = 0, j = 0;
  int leni, lenj;

  leni = sizeof matrix / sizeof matrix[0][0];
  lenj = sizeof (matrix[0]) / sizeof (matrix[0][0]);

for(i = 0 ; i < leni ; i++)
{
    for(j = 0 ; j < lenj ; j++)
    {
        printf("%d  ", matrix[i][j]);
    }
    printf("\n");
}
printf("\n");

}

and a matrix:

int A[2][3] = {{1, 3, 4},
               {2, 0, 1},

and this method writes me my matrix only for 2 x 3, I was making a research and leni and lenj return quite bad numbers, problem will be in void vypisMatice(int matrix[][3]){ where I am declaring matrix with number of columns, but without that number, compiler won't compile my app. I just want to know how to adjust my method to accept matrix of any size when printing, like printMatrix(A);?

Thanks


Note that in a function declaration, any parameter of array type is converted to a parameter of the corresponding pointer type.

void printMatrix(int matrix[][3]);

declares the same function as

void printMatrix(int (*matrix)[3]);

The matrix parameter is a pointer to an array of three int. When you pass A into the function what is actually being passed is a pointer to the first element of the array, i.e. a int (*)[3].

This calculation sizeof matrix / sizeof matrix[0][0] gives the size of a pointer divided by the size of an int which is nothing to do with the number of members of the original array. This number cannot be determined from the parameters that you pass in. You need to explicitly pass the dimensions of your outer array into the function.

When you declare an array (even in a function declaration where it is going to be converted to a pointer type), the array element type must be a complete type. If you don't supply an integer constant type for an array type, that array has an incomplete type. In your case, because int[] is an complete type, the type int[][] is illegal, you must supply all but at most the top dimension in a multilevel array of arrays type.

If you using C99 features (note, not C++ compatible) then you can use a variable length array (VLA), but in the function defintion the length of the inner array of the array parameter must still be passed as an additional parameter.

E.g.

void printMatrix(int leni, int lenj, int[][lenj])
{
    // ...
}


leni = sizeof matrix / sizeof matrix[0][0]; it doesn't calculates the number of rows. It calculates nothing in fact.

The best way is to provide leni and lenj explicitly to function as additional parameters, like void printMatrix(int *matrix, int leni, int lenj).

Also you should know that there is no way to determine the size of some random array because it is just a pointer without any meta-information.


If you need to accept an arbitrarily sized matrix, you're going to need to include parameters to printMatrix() that tell it what size your array is. Something like:

void printMatrix(int *matrix, int rows, int columns)

Then you can use the rows and columns parameters to control your loops inside the print routine, instead of calculating them yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜