如何传递多维数组? [英] How to pass multi-dimensional array?
问题描述
假设你在main()中有一个二维数组(矩阵)并且你想要
将它传递给一个函数进行一些处理,你通常把它传递给
a指向第一个元素的指针。但是,从函数中,你如何使用
索引表示法?
示例:
int matrix_check (int * m)
{
/ *我们如何使用m [i] [j]类型的表示? * /
}
int main()
{
int mat [10] [ 10];
if(matrix_check(& matrix [0] [0]))
{
/ * do东西* /
}
...
}
为什么Tea说:
假设你在main()中有一个二维数组(矩阵),你想要
将它传递给函数进行一些处理,你通常将它作为指向第一个元素的
a指针传递给它。但是,从功能来看,你如何使用
的索引表示法?
您已经在comp.lang.c常见问题解答中询问了Q6.18的变体,您可以在 =nofollowhref =http://c-faq.com/index.htmltarget =_ blank> http://c-faq.com/index.html
我建议你阅读相应的答案,然后在这里跟进,如果
你有任何进一步的问题。
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
10月22日,8月39日,为什么茶< ytl ... @ gmail.comwrote:
假设你在main()中有一个二维数组(矩阵)并且你想要
将它传递给一个函数进行一些处理,你通常将它作为
a指向第一个元素的指针。但是,从函数中,你如何使用
索引表示法?
示例:
int matrix_check (int * m)
{
/ *我们如何使用m [i] [j]类型的表示? * /
}
int main()
{
int mat [10] [10];
if(matrix_check(& matrix [0] [0]))
{
/ *做点什么* /
}
...
} - -
- -
如果你想使用m作为2-
$ b,你不要将矩阵声明为int * m $ b dimention数组,你应该将它声明为指针的指针 - int ** m。
并且,在函数中,你可以自由地使用这个指针的指针,如果你想要的话br />
使用这个数组作为m [2] [3],你可以通过这种方式访问m [1] [2] :( * m)
+(2 * 1 +2)依此类推。
o,顺便说一句,你不能像你一样调用你的函数,你可以称之为
as:matrix_check(matrix)
而且,在C中,有些规则不是规则。我刚才说的是原始规则。你
也可以使用"(matrix_check(& matrix [0] [0]))"在功能上,你可以
访问任何变量(m [1] [2]),因为:m + 2 * 1 + 2.
< blockquote>你可以用这种简单的方法将它传递给一个函数:
int matrix_check(int m [] [10])
{
/ * chech二维数组 - m * /
返回your_return_value;
}
int main(int argc,char * argv [])
{
int mat [10] [10];
if(matrix_check(mat))
{
/ *用垫子或别的东西做你的工作* /
}
...
}
为什么Tea写道:
假设你有main()中的二维数组(矩阵)你想要
将它传递给一个函数进行一些处理,你通常把它传递给
a指向第一要素。但是,从函数中,你如何使用
索引表示法?
示例:
int matrix_check (int * m)
{
/ *我们如何使用m [i] [j]类型的表示? * /
}
int main()
{
int mat [10] [ 10];
if(matrix_check(& matrix [0] [0]))
{
/ * do一些东西* /
}
...
}
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[i][j] type of representation? */
}
int main()
{
int mat[10][10];
if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...
}
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.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On 10 22 , 8 39 , Why Tea <ytl...@gmail.comwrote: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[i][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.
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 */
}
...
}
Why Tea wrote: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[i][j] type of representation? */
}
int main()
{
int mat[10][10];
if (matrix_check(&matrix[0][0]))
{
/* do something */
}
...
}
这篇关于如何传递多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!