How to pass multi-dimensional array?

W

Why Tea

Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

Example:

int matrix_check(int *m)
{
/* how can we use m[j] type of representation? */
}

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...
}
 
R

Richard Heathfield

Why Tea said:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

You have asked a variant of Q6.18 in the comp.lang.c FAQ, which you can
find at http://c-faq.com/index.html

I suggest that you read the appropriate answer, and then follow up here if
you have any further questions.
 
Z

zhangqw.office

Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

Example:

int matrix_check(int *m)
{
/* how can we use m[j] type of representation? */

}

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...



}- -

- -


you do not declare matrix as "int *m",if you want to use m as 2-
dimention array,you should declare it as pointer's pointer--int **m.
And,in function,you can use this pointer's pointer freely,if you want
use this array as m[2][3],you can access m[1][2] by this way: (*m)
+(2*1+2) and so on .
o,BTW,you can not call your function as you do,you can call it
as :matrix_check(matrix)
And,in C,some rule is not rule.What i just said is original rule. You
can also use "(matrix_check(&matrix[0][0]))" and in function,you can
access any variable (m[1][2]) as this : m+2*1+2.
 
Z

zhengcao.jin

You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

int main(int argc, char *argv[])
{
int mat[10][10];

if (matrix_check(mat))
{
/* do your work with mat or something else */
}
...
}
 
M

MisterE

Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation

You can't pass an array to a function. Ever. You can only pass a pointer. If
you have array[10][10], its basically the same as array[100]. The easiest
way to do what you want is to pass a pointer to the first element, and a
value indicating the 2nd dimensional size of the array.

int matrix_check(int *val, int width)
{
m[i+(j*width)] = whatever
}

matrix_check(mat,10);
 
P

Prayag Narula

you do not declare matrix as "int *m",if you want to use m as 2-
dimention array,you should declare it as pointer's pointer--int **m.
And,in function,you can use this pointer's pointer freely,if you want
use this array as m[2][3],you can access m[1][2] by this way: (*m)
+(2*1+2) and so on .


I think you mean *(m+(2*1)+2))

for example:

#define ROW 3
#define COL 4

#include<stdio.h>

void call_function(int** ptr);

int main(void)
{
int mat[ROW][COL] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 10,11,12};

call_function((int**)mat);

}

void call_function(int **m)
{
printf("%d",*(m+ ((2*COL)+ 3))); // To access 2*3
}

OUTPUT: 12

Hope it helps
 
R

Richard Heathfield

(e-mail address removed) said:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

Example:

int matrix_check(int *m)
{
/* how can we use m[j] type of representation? */

}

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))


you do not declare matrix as "int *m",if you want to use m as 2-
dimention array,you should declare it as pointer's pointer--int **m.


No, you shouldn't: int [10][10] is not an int **, and must not be treated
like one.
 
R

Richard Heathfield

Prayag Narula said:

void call_function(int** ptr);

int main(void)
{
int mat[ROW][COL] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 10,11,12};

call_function((int**)mat);

Casts are not magic wands. Get the right type for call_function(), and you
won't need to cast. In this case, you want int (*)[COL], not int **.
Hope it helps

Correct answers help far more than incorrect answers. Try to get into the
habit of giving correct answers.
 
M

Martin Wells

Why Tea:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element.


If the multi-dimensional array is of known dimensions, I'd go with:

void Func( int (*const p)[7][3] )
{
#define arr (*p)



arr[0][2] = 3;



#undef arr
}

int main(void)
{
int arr[7][3];

Func(&arr);

return 0;
}

Otherwise, go with

#include <stddef.h>

void Func( int (*const p)[3], size_t len )
{
p[len -1][2] = 3;
}

int main(void)
{
int arr[7][3];

Func(arr,7);

return 0;
}


Martin
 
J

James Kuyper Jr.

You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

int main(int argc, char *argv[])
{
int mat[10][10];

if (matrix_check(mat))
{
/* do your work with mat or something else */
}
...
}

Of course, that only works when both dimensions of the array are fixed.
If the first dimension is variable, then the actual length of that
dimension should be passed to matrix_check. That works because 'm' is
really a pointer type pointing at int[10], and it can point at an
arbitrarily long array of int[10]. If the second dimension is variable,
it gets more complicated.

A more general approach that became feasible in C99 uses the new
variable length array feature to deal with that problem:

int matrix_check(int rows, int cols, int array[rows][cols])
{
// Check 2-dimensional array
return your_return_value;
}

int main(int argc, char *argv[])
{
int mat[10][10];

// fill in contents of mat
if(matrix_check(10, 10, mat))
{
// work with mat
}
// more stuff
}
 
B

Barry Schwarz

Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation

You can't pass an array to a function. Ever. You can only pass a pointer. If
you have array[10][10], its basically the same as array[100]. The easiest

This is you second post today with incorrect or misleading
information. Please desist.

While array[10][10] may hold the same number of scalar elements as
array[100], they are not basically the same at all.
way to do what you want is to pass a pointer to the first element, and a
value indicating the 2nd dimensional size of the array.

And this is hardly the easiest.
int matrix_check(int *val, int width)
{
m[i+(j*width)] = whatever

}

matrix_check(mat,10);
 
B

Barry Schwarz

Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

int matrix_check(int *m)
{
/* how can we use m[j] type of representation? */

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...

you do not declare matrix as "int *m",if you want to use m as 2-
dimention array,you should declare it as pointer's pointer--int **m.


If the argument being passed is in fact defined as a 2D array, then
using an int** is incorrect both syntactically and logically.
And,in function,you can use this pointer's pointer freely,if you want
use this array as m[2][3],you can access m[1][2] by this way: (*m)
+(2*1+2) and so on .

m[j] is an int. (*m) is an int*. Adding an integer value to it
does not change its type. The two types are completely different.
o,BTW,you can not call your function as you do,you can call it
as :matrix_check(matrix)
And,in C,some rule is not rule.What i just said is original rule. You
can also use "(matrix_check(&matrix[0][0]))" and in function,you can
access any variable (m[1][2]) as this : m+2*1+2.- Hide quoted text -

- Show quoted text -
 
P

Prayag Narula

Casts are not magic wands. Get the right type for call_function(), and you
won't need to cast. In this case, you want int (*)[COL], not int **.

My mistake. gcc summoned a warning and I just type casted and the
warning disappeared, I assumed that it was causing the problem. Should
have been more careful. Note to self: should desist from type casting
as the first move to solve problems.
Correct answers help far more than incorrect answers. Try to get into the
habit of giving correct answers.

Apologies

cheers
 
R

Richard Heathfield

Prayag Narula said:
Casts are not magic wands. Get the right type for call_function(), and
you won't need to cast. In this case, you want int (*)[COL], not int **.

My mistake. gcc summoned a warning and I just type casted and the
warning disappeared, I assumed that it was causing the problem. Should
have been more careful. Note to self: should desist from type casting
as the first move to solve problems.

Good plan.
Apologies

Well done. score += 10;
 
C

Charlie Gordon

Why said:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation?

Example:

int matrix_check(int *m)
{
/* how can we use m[j] type of representation? */
}

int main()
{
int mat[10][10];

if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...
}


You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}


Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation is that sizeof(m) is not the size of
the matrix.
int main(int argc, char *argv[])
{
int mat[10][10];

if (matrix_check(mat))
{
/* do your work with mat or something else */
}
...
}
 
K

Keith Thompson

Charlie Gordon said:
<[email protected]> a écrit dans le message de
(e-mail address removed)... [...]
You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation is that sizeof(m) is not the size of
the matrix.
[...]

The only other subtle trap is that the first ``10'' is silently
ignored.
 
P

pete

Keith said:
Charlie Gordon said:
<[email protected]> a écrit dans le message de (e-mail address removed)... [...]
You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation
is that sizeof(m) is not the size of the matrix.
[...]

The only other subtle trap is that the first ``10'' is silently
ignored.

int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.
 
K

Keith Thompson

Barry Schwarz said:
Suppose you have a 2-dimensional array (matrix) in main() and you want
to pass it to a function to do some processing, you usually pass it as
a pointer to the first element. But, from the function, how do you use
the index notation

You can't pass an array to a function. Ever. You can only pass a pointer. If
you have array[10][10], its basically the same as array[100]. The easiest

This is you second post today with incorrect or misleading
information. Please desist.

While array[10][10] may hold the same number of scalar elements as
array[100], they are not basically the same at all.
[...]

They're likely (guaranteed, I think) to be represented the same way.
 
K

Keith Thompson

pete said:
Keith said:
Charlie Gordon said:
<[email protected]> a écrit dans le message de (e-mail address removed)... [...]
You can do with this simple way to pass it to a function:

int matrix_check(int m[][10])
{
/* chech the 2-dimensional array - m */
return your_return_value;
}

Or even simpler to grasp with:

int matrix_check(int m[10][10]) { ... }

The only subtle trap with this notation
is that sizeof(m) is not the size of the matrix.
[...]

The only other subtle trap is that the first ``10'' is silently
ignored.

int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.

Exactly. IMHO, allowing array notation for parameters that are really
pointers was a language design mistake.
 
J

James Kuyper

pete wrote:
....
int matrix_check(int (*m)[10]) { ... }

I like the pointer notation for the parameter,
because that makes it more obvious what the parameter really is.

If I declared it the way you suggest, the compiler I use most frequently
would insist on reminding me that I should declare 'm' to be const. It
issues no such reminder when I use "int m[][10]", even though that's
functionally equivalent to your declaration. I can't turn off that
reminder without turning off other 'const'-related reminders that I
actually find useful.

It's annoying how stupid things like that can affect your programming style.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top