在C中,当我不知道数组的维数是什么时,我可以将多维数组作为单个参数传递给函数吗?
另外,我的多维数组可能包含字符串以外的类型。
答案 0 :(得分:31)
将显式指针传递给第一个元素,并将数组维度作为单独的参数。例如,要处理int:
的任意大小的2-d数组void func_2d(int *p, size_t M, size_t N)
{
size_t i, j;
...
p[i*N+j] = ...;
}
将被称为
...
int arr1[10][20];
int arr2[5][80];
...
func_2d(&arr1[0][0], 10, 20);
func_2d(&arr2[0][0], 5, 80);
同样的原则适用于高维数组:
func_3d(int *p, size_t X, size_t Y, size_t Z)
{
size_t i, j, k;
...
p[i*Y*Z+j*Z+k] = ...;
...
}
...
arr2[10][20][30];
...
func_3d(&arr[0][0][0], 10, 20, 30);
答案 1 :(得分:19)
您可以使用任何数据类型执行此操作。只需将其指向指针:
typedef struct {
int myint;
char* mystring;
} data;
data** array;
但是不要忘记你仍然需要malloc变量,它确实有点复杂:
//initialize
int x,y,w,h;
w = 10; //width of array
h = 20; //height of array
//malloc the 'y' dimension
array = malloc(sizeof(data*) * h);
//iterate over 'y' dimension
for(y=0;y<h;y++){
//malloc the 'x' dimension
array[y] = malloc(sizeof(data) * w);
//iterate over the 'x' dimension
for(x=0;x<w;x++){
//malloc the string in the data structure
array[y][x].mystring = malloc(50); //50 chars
//initialize
array[y][x].myint = 6;
strcpy(array[y][x].mystring, "w00t");
}
}
取消分配结构的代码看起来很相似 - 不要忘记在malloced的所有内容上调用free()! (另外,在强大的应用程序中,您应该check the return of malloc()。)
现在假设你要将它传递给一个函数。你仍然可以使用双指针,因为你可能想对数据结构进行操作,而不是指向数据结构指针的指针:
int whatsMyInt(data** arrayPtr, int x, int y){
return arrayPtr[y][x].myint;
}
使用以下方法调用此函数:
printf("My int is %d.\n", whatsMyInt(array, 2, 4));
输出:
My int is 6.
答案 2 :(得分:18)
您可以将您的功能声明为:
f(int size, int data[][size]) {...}
然后,编译器将为您执行所有指针运算。
请注意,尺寸大小必须在之前显示在数组本身之前。
GNU C允许参数声明转发(如果你真的需要在数组之后传递维度):
f(int size; int data[][size], int size) {...}
第一个维度,虽然你也可以作为参数传递,但对于C编译器来说是无用的(即使对于sizeof运算符,当应用于数组传递时,参数将总是视为指向第一个元素的指针)。
答案 3 :(得分:0)
在C中,我不知道数组的尺寸是多少时,可以将多维数组作为单个参数传递给函数吗?
否
如果通过“单个参数”表示只传递数组而不传递数组维数,那么您不能。至少不是真正的多维数组。
您可以将维与数组一起放入结构中,并声明您正在传递“单个参数”,但这实际上只是将多个值包装到单个容器中,并将该容器称为“一个参数”。
您可以通过传递尺寸本身和如下数组来传递已知类型和尺寸数量但未知尺寸的数组:
void print2dIntArray( size_t x, size_t y, int array[ x ][ y ] )
{
for ( size_t ii = 0, ii < x; ii++ )
{
char *sep = "";
for ( size_t jj = 0; jj < y; jj++ )
{
printf( "%s%d", sep, array[ ii ][ jj ] );
sep = ", ";
}
printf( "\n" );
}
}
您将这样调用该函数:
int a[ 4 ][ 5 ];
int b[ 255 ][ 16 ];
...
print2dIntArray( 4, 5, a );
....
printt2dIntArray( 255, 16, b );
类似地,一个3维数组,例如struct pixel
:
void print3dPixelArray( size_t x, size_t y, size_t z, struct pixel pixelArray[ x ][ y ][ z ] )
{
...
}
或一维double
数组:
void print1dDoubleArray( size_t x, double doubleArray[ x ] )
{
...
}
但是...
但是,有可能将“指针数组传递给指向X
类型的数组的指针数组”结构,这些结构通常被错误地标记为“多维数组”作为单个参数,只要因为基本类型X
具有一个哨兵值,可用于指示类型X
的最终的最低级别的一维数组的结尾。
例如,传递给char **argv
的{{1}}值是指向main()
的指针数组的指针。 char
指针的初始数组以char *
标记值结尾,而NULL
指针数组引用的每个char
数组以char *
字符值结尾的NUL
。
例如,如果您可以使用'\0'
作为标记值,因为实际数据不会永远成为NAN
,则可以打印NAN
像这样:
double **
答案 4 :(得分:-2)
int matmax(int **p, int dim) // p- matrix , dim- dimension of the matrix
{
return p[0][0];
}
int main()
{
int *u[5]; // will be a 5x5 matrix
for(int i = 0; i < 5; i++)
u[i] = new int[5];
u[0][0] = 1; // initialize u[0][0] - not mandatory
// put data in u[][]
printf("%d", matmax(u, 0)); //call to function
getche(); // just to see the result
}