How write a method that receive a array of int?

Hi,

void my_method(int matrix[][]){
matrix[0][0] = 0;
}

Why this does not work?
Why this does not work?


What did the compiler error message say?
Suppose you are a compiler and someone asks you to compile this:

1
2
3
4
void my_method(int matrix[][])
{
    matrix[1][1]=5;
}

What would you do? Remember, no matter how many dimensions an array has, you (as the compiler) see it as a single "line" of data.

So, if the array was a 3x3 matrix like this:

{1,1,1},
{1,1,1},
{1,1,1}

you would perceive it like this: {1,1,1,1,1,1,1,1,1} and you would interpret matrix[1][1] as matrix[4].

But if the array was a 3x2 matrix, like this:

{1,1},
{1,1},
{1,1}

you would perceive it like this: {1,1,1,1,1,1} and you would interpret matrix[1][1] as matrix[3].

As you see, you require additional information to understand what your programmer means with matrix[i][j].

But if your programmer did it like this:

1
2
3
4
void my_method(int matrix[][n])
{
    matrix[1][1]=5;
}

where n is a (literal) constant, I suppose you could figure out what he meant ;)
Last edited on
Topic archived. No new replies allowed.