我是C的新手,我想这样做:
void init(int array[][variable]);
编译器报告此错误:
error: expected expression
如何将具有固定行数但具有可变列数的二维数组传递给函数?
答案 0 :(得分:2)
你可以这样做:
void init1(int variable_nr_of_columns, int *array);
或
void init2(int variable_nr_of_columns, int array[][variable_nr_of_columns]);
这是一个简单的程序来演示用法:
#include <stdio.h>
#include <stdlib.h>
#define NR_OF_ROWS 2
void init1(int variable_nr_of_columns, int *array)
{
int i,j;
for(i=0; i<NR_OF_ROWS; i++)
{
for(j=0; j<variable_nr_of_columns; j++)
{
*(array + i*variable_nr_of_columns+ j) = i*variable_nr_of_columns + j;
// FIRTS ROW
//[0][0] 0 i*nr_of_columns + j
//[0][1] 1
//[0][2] 2
// SECOND ROW
//[1][0] 3 i*nr_of_columns + j
//[1][1] 4
//[1][2] 5
}
}
}
void init2(int variable_nr_of_columns, int array[][variable_nr_of_columns])
{
int i, j;
for(i=0; i<NR_OF_ROWS; i++)
{
for(j=0; j<variable_nr_of_columns ;j++)
{
array[i][j] = i*variable_nr_of_columns + j;
// FIRTS ROW
//[0][0] 0
//[0][1] 1
//[0][2] 2
// SECOND ROW
//[1][0] 3
//[1][1] 4
//[1][2] 5
}
}
}
int main()
{
int i,j;
int var_nr_of_columns = 3; // variable number of columns
int *ptr1; // pointer to table1 (to show how to init a pointer to table1 )
int (*ptr2)[var_nr_of_columns]; // pointer to table2 (to show how to init a pointer to table2 )
int table1[NR_OF_ROWS][var_nr_of_columns]; // declare table1
int table2[NR_OF_ROWS][var_nr_of_columns]; // declare table2
ptr1 = &table1[0][0]; // pointer `ptr1` points to first element in the table1
ptr2 = table2; // pointer `ptr2` points to first element in the table2
// arrays are layed out contiguously in memory, so the compiler must know all
// but the first dimension to be able to calculate offsets into that block of memory.
// --------------------------------------------------------------------------------
// 1a. call `init1` function
init1(var_nr_of_columns, ptr1); // or `init1(var_nr_of_columns, &table1[0][0]);`
// 1b. print initialized `table1`
printf("table1[NR_OF_ROWS][var_nr_of_columns]\n\n");
for(i=0;i< NR_OF_ROWS;i++)
for(j=0;j<var_nr_of_columns;j++)
printf("row=%d col=%d table1[ %d ][ %d ] = %d \n", i, j, i, j, table1[i][j] );
printf("\n");
//---------------------------------------------------------------------------------
// 2a. call `1init2` function
init2(var_nr_of_columns, ptr2); // or simply `init2(var_nr_of_columns, table2);`
// 2b. print initialized `table2`
printf("table2[NR_OF_ROWS][var_nr_of_columns]\n\n");
for(i=0;i< NR_OF_ROWS;i++)
for(j=0;j<var_nr_of_columns;j++)
printf("row=%d col=%d table2[ %d ][ %d ] = %d \n", i, j, i, j, table2[i][j] );
return 0;
}
输出:
table1[NR_OF_ROWS][var_nr_of_columns]
row=0 col=0 table1[ 0 ][ 0 ] = 0
row=0 col=1 table1[ 0 ][ 1 ] = 1
row=0 col=2 table1[ 0 ][ 2 ] = 2
row=1 col=0 table1[ 1 ][ 0 ] = 3
row=1 col=1 table1[ 1 ][ 1 ] = 4
row=1 col=2 table1[ 1 ][ 2 ] = 5
table2[NR_OF_ROWS][var_nr_of_columns]
row=0 col=0 table2[ 0 ][ 0 ] = 0
row=0 col=1 table2[ 0 ][ 1 ] = 1
row=0 col=2 table2[ 0 ][ 2 ] = 2
row=1 col=0 table2[ 1 ][ 0 ] = 3
row=1 col=1 table2[ 1 ][ 1 ] = 4
row=1 col=2 table2[ 1 ][ 2 ] = 5