【问题标题】:How to pass a 2 dimensional array with the variable number of columns into a function?如何将具有可变列数的二维数组传递给函数?
【发布时间】:2017-12-10 00:49:54
【问题描述】:

我是 C 新手,我想这样做:

void init(int array[][variable]);

编译器报此错误:

error: expected expression

如何将行数固定但列数可变的二维数组传递给函数?

【问题讨论】:

  • 所以你的数组有固定大小的列和可变大小的行?

标签: c arrays multidimensional-array


【解决方案1】:

你可以这样做:

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 

【讨论】:

  • 什么是*list? @sd7
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 2020-11-01
相关资源
最近更新 更多