C 如何将列数可变的二维数组传递给函数?
我是C新手,我想这样做:C 如何将列数可变的二维数组传递给函数?,c,arrays,multidimensional-array,C,Arrays,Multidimensional Array,我是C新手,我想这样做: void init(int array[][variable]); 编译器报告此错误: error: expected expression 如何将具有固定行数但具有可变列数的二维数组传递到函数中?您可以按以下方式执行: void init1(int变量\u nr\u列,int*数组) 或 void init2(int-variable_nr_of_columns,int-array[][variable_nr_of_columns]) 这是一个演示用法的简单程序:
void init(int array[][variable]);
编译器报告此错误:
error: expected expression
如何将具有固定行数但具有可变列数的二维数组传递到函数中?您可以按以下方式执行:
void init1(int变量\u nr\u列,int*数组)代码>
或
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;
}
所以你的数组有固定大小的列和可变大小的行?什么是*列表@sd7
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