C 操作函数中的多维数组
我在这里读了很多东西并尝试了很多,但是我找不到一种方法来将多维数组传递给C中的函数,更改一些值并以某种方式返回新数组。 找到一种方法将该数组进一步传递给另一个函数并执行相同的操作,这一点很重要 我想找到一种方法将数组传递给一个函数,然后将它从第一个函数传递到第二个函数,在那里做一些事情(可能打印,可能更改值),然后再次将它传递给第一个函数,最后在main中使用该数组 我最后一次尝试是:C 操作函数中的多维数组,c,arrays,function,multidimensional-array,parameters,C,Arrays,Function,Multidimensional Array,Parameters,我在这里读了很多东西并尝试了很多,但是我找不到一种方法来将多维数组传递给C中的函数,更改一些值并以某种方式返回新数组。 找到一种方法将该数组进一步传递给另一个函数并执行相同的操作,这一点很重要 我想找到一种方法将数组传递给一个函数,然后将它从第一个函数传递到第二个函数,在那里做一些事情(可能打印,可能更改值),然后再次将它传递给第一个函数,最后在main中使用该数组 我最后一次尝试是: void func(int multarray[][columns]){ multarray[0][0
void func(int multarray[][columns]){
multarray[0][0]=9;
}
int main(){
int rows;
int columns;
int multarray[rows][columns];
func(multarray);
return 0;
}
我也试过:
void func(int multarray[rows][columns]){
multarray[0][0]=9;
}
int main(){
int rows;
int columns;
int multarray[rows][columns];
func(multarray);
return 0;
}
int
getid(int row, int x, int y) {
return (row*x+y);
}
void
printMatrix(int*arr, int row, int col) {
for(int x = 0; x < row ; x++) {
printf("\n");
for (int y = 0; y <col ; y++) {
printf("%d ",arr[getid(row, x,y)]);
}
}
}
main()
{
int arr[2][2] = {11,12,21,22};
int row = 2, col = 2;
printMatrix((int*)arr, row, col);
}
我也试过:
void func(int multarray[rows][columns]){
multarray[0][0]=9;
}
int main(){
int rows;
int columns;
int multarray[rows][columns];
func(multarray);
return 0;
}
int
getid(int row, int x, int y) {
return (row*x+y);
}
void
printMatrix(int*arr, int row, int col) {
for(int x = 0; x < row ; x++) {
printf("\n");
for (int y = 0; y <col ; y++) {
printf("%d ",arr[getid(row, x,y)]);
}
}
}
main()
{
int arr[2][2] = {11,12,21,22};
int row = 2, col = 2;
printMatrix((int*)arr, row, col);
}
int
getid(整数行,整数x,整数y){
返回(行*x+y);
}
无效的
打印矩阵(int*arr,int行,int列){
对于(int x=0;x
对于(int y=0;y不完全确定问题是什么,但这是可行的(并打印值“9”):
#包括
#定义第10行
#定义第10列
void func2(int multarray[][列]){
multarray[1][4]=10;
}
void func1(int multarray[][列]){
multarray[0][3]=9;
func2(multarray);
}
int main(){
int multarray[行][列];
func1(multarray);
printf(“%d\n”,multarray[0][3]);
printf(“%d\n”,multarray[1][4]);
返回0;
}
请注意,数组在传递给函数时会衰减为指针。将数组视为指向内存的指针是很有帮助的。然后很容易将2d数组视为指向指针内存的指针(类似于)
这是行不通的
int arr[2][2] = {11,12,21,22}; //not going to work
但这对我来说很好
1 #include <stdio.h>
2
3
4 main()
5 {
6 int arr[2][2] = {{11,1},{2,21}};
7 int row = 2, col = 2;
8 int i,j;
9
10 for(i=0;i<row;i++)
11 for(j=0;j<col;j++)
12 printf("%d - ",arr[i][j]);
13
14
15 }
1#包括
2.
3.
4主要内容()
5 {
6 int arr[2][2]={{11,1},{2,21};
7整数行=2,列=2;
8 int i,j;
9
10对于(i=0;i对于二维数组,我将为其定义一个类型
typedef int my2DArray_t[ROWS][COLUMNS];
然后你可以声明这种类型的变量和指向它们的指针,这样可以更容易地传递信息
void someFuncOther (my2DArray_t *someArray)
{
/* Set some values in array */
(*someArray)[1][1] = 4;
}
void someFunc (my2DArray_t *someArray)
{
/* Set some values in array */
(*someArray)[1][0] = 7;
/* Have someFuncOther do some more work */
someFuncOther (someArray);
}
int main (void)
{
/* This is the actual array */
my2DArray_t myArray;
/* Set some values in array */
myArray[0][2] = 6;
/* Have someFunc do some work on myArray by passing a pointer to it */
someFunc (&myArray);
}
有几件事需要记住:
将数组表达式作为参数传递给函数时,它将从类型为“N-element array ofT
”的表达式转换为“pointer toT
”,表达式的值将是数组第一个元素的地址。被调用的函数接收指针值
[]
运算符可用于数组或指针类型的表达式;IOW,给定声明int a[10];int*p=a;
,则p[i]
和a[i]
引用同一元素
声明接受VLA作为参数的函数时,必须在声明数组之前声明指定维度的参数
因此,对于操纵2D VLA的函数,您可以编写如下内容
void foo( size_t rows, size_t cols, int (*multiarray)[cols] ) // or multiarray[][cols]
{
size_t i, j;
for ( i = 0; i < rows; i++ )
for ( j = 0; j < cols; j++ )
multiarray[i][j] = some_value();
}
在foo
中对数组内容所做的任何更改都将反映在bar
和main
中
VLA可能很有用,但也有其局限性。它们不能声明为静态
,也不能在函数外部定义。它们不能使用{}
-风格的初始化语法。此外,从2011年的标准开始,VLA支持现在是可选的,因此您不能依赖它们在任何地方都得到支持
如果您没有可用的VLA,并且直到运行时才知道数组大小,则必须使用动态内存分配(malloc
或calloc
),并且传递给函数的类型将不同:
void foo( size_t rows, size_t cols, int **multiarray )
{
size_t i, j;
for ( i = 0; i < rows; i++ )
for ( j = 0; j < cols; j++ )
multiarray[i][j] = some_value();
}
void bar( size_t rows, size_t cols, int **multiarray )
{
foo( rows, cols, multiarray );
}
int main( void )
{
size_t rows;
size_t cols;
int **multiarray = NULL;
... // get rows and cols
// allocate memory for pointers to each row
multiarray = malloc( sizeof *multiarray * rows );
if ( multiarray )
{
size_t i;
// allocate each row
for ( i = 0; i < rows; i++ )
{
multiarray[i] = malloc( sizeof *multiarray[i] * cols );
if ( !multiarray[i] )
break;
}
if ( i < rows )
{
// malloc failed for one of the multiarray rows; we need to
// free whatever memory has already been allocated and exit
while ( i-- )
free( multiarray[i] );
free( multiarray );
exit(0);
}
}
bar ( rows, cols, multiarray );
...
if ( multiarray )
{
size_t i;
for ( i = 0; i < rows; i++ )
free( multiarray[i] );
free( multiarray );
}
}
void foo(大小行、大小列、整数**多数组)
{
尺寸i,j;
对于(i=0;i
这种方法的一个缺点是,分配的内存不能保证是连续的(即,内存中的行不会相邻)。如果这很重要,您必须使用另一种方法。您不需要单独分配行和列,而是在一个块中分配所有内容,并手动映射数组索引:
void foo( size_t rows, size_t cols, int *fakemultiarray )
{
size_t i, j;
for ( i = 0; i < rows; i++ )
for ( j = 0; j < rows; j++ )
fakemultiarray[ i * rows + j ] = some_value();
}
void bar( size_t rows, size_t cols, int *fakemultiarray )
{
foo( rows, cols, fakemultiarray );
}
int main( void )
{
size_t rows;
size_t cols;
int *fakemultiarray = NULL;
... // get rows and cols
fakemultiarray = malloc( sizeof *fakemultiarray * rows * cols );
if ( fakemultiarray )
bar( rows, cols, fakemultiarray );
...
free( fakemultiarray );
}
void foo(大小行、大小列、int*fakemultiarray)
{
尺寸i,j;
对于(i=0;i
在本例中,我们为所有元素分配了一个足够大的缓冲区,但我们必须将其作为1D数组进行索引,将索引计算为i*rows+j
您没有定义行和列吗?@Lucas没有,我有。我只是编辑了问题。在第二个示例中,您从未定义行和列的大小。此外,在全局定义的主阴影。@Lucas问题已编辑。。这不是