This question already has an answer here:
这个问题在这里已有答案:
- Why do I need to use type** to point to type*? 3 answers
为什么我需要使用类型**指向类型*? 3个答案
I have a function that supposed to take 2D array as an argument, my code looks like this --
我有一个假设将2D数组作为参数的函数,我的代码看起来像这样 -
#include <stdio.h>
#include <stdlib.h>
void func(double**, int);
int main()
{
double m[3][3] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
func(m, 3);
}
void func(double **m, int dim)
{
int i, j ;
for(i = 0 ; i < dim ; i++)
{
for(j = 0 ; j < dim ; j++)
printf("%0.2f ", m[i][j]);
printf("\n");
}
}
Then the compiler says --
然后编译器说 -
test.c: In function ‘main’:
test.c:9:2: warning: passing argument 1 of ‘func’ from incompatible pointer type [enabled by default]
func(m, 3);
^
test.c:4:6: note: expected ‘double **’ but argument is of type ‘double (*)[3]’
void func(double**, int);
^
But when I say --
但是当我说 -
int main()
{
int i, j;
double m[3][3] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
double **m1 ;
m1 = (double**)malloc(sizeof(double*) * 3);
for(i = 0 ; i < 3 ; i++)
{
m1[i] = (double*)malloc(sizeof(double) * 3);
for(j = 0 ; j < 3 ; j++)
m1[i][j] = m[i][j] ;
}
func(m1, 3);
for(i = 0 ; i < 3 ; i++) free(m1[i]);
free(m1);
}
It compiles and runs.
它编译并运行。
Is there any way I can make func()
to take both the statically/dynamically defined 2D array ? I am confused since I am passing the pointer m
, why it is not correct for the first case ?
有什么方法可以使func()同时采用静态/动态定义的2D数组?我很困惑,因为我传递指针m,为什么第一种情况不正确?
does it mean that I need to write two separate functions for two different types of arguments?
这是否意味着我需要为两种不同类型的参数编写两个单独的函数?
1 个解决方案
#1
5
Your dynamic allocation for 2d array is not correct. Use it as:
您对2d数组的动态分配不正确。用它作为:
double (*m1)[3] = malloc(sizeof(double[3][3]));
And then it will work.
然后它会工作。
Moreover change the function prototype to:
而且将函数原型更改为:
void func(double m[][3], int dim)
Another way is to use a 1-D array of size w * h
instead of 2-D array.
另一种方法是使用大小为w * h的1-D阵列而不是2-D阵列。
From comment of @TheParamagneticCroissant c99 onwards, you can also VLA's and make both your dimensions variable. (You'll need to allocate the 2D array properly though)
从@TheParamagneticCroissant c99的评论开始,您还可以使用VLA并使您的维度变量。 (你需要正确分配2D数组)
Change function signature to:
将功能签名更改为:
void func(int dim, double[dim][dim]); /* Second arg is VLA whose dimension is first arg */
/* dim argument must come before the array argument */
#1
5
Your dynamic allocation for 2d array is not correct. Use it as:
您对2d数组的动态分配不正确。用它作为:
double (*m1)[3] = malloc(sizeof(double[3][3]));
And then it will work.
然后它会工作。
Moreover change the function prototype to:
而且将函数原型更改为:
void func(double m[][3], int dim)
Another way is to use a 1-D array of size w * h
instead of 2-D array.
另一种方法是使用大小为w * h的1-D阵列而不是2-D阵列。
From comment of @TheParamagneticCroissant c99 onwards, you can also VLA's and make both your dimensions variable. (You'll need to allocate the 2D array properly though)
从@TheParamagneticCroissant c99的评论开始,您还可以使用VLA并使您的维度变量。 (你需要正确分配2D数组)
Change function signature to:
将功能签名更改为:
void func(int dim, double[dim][dim]); /* Second arg is VLA whose dimension is first arg */
/* dim argument must come before the array argument */