首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将具有可变列数的二维数组传递给函数?

问如何将具有可变列数的二维数组传递给函数?
EN

Stack Overflow用户
提问于 2017-12-10 08:49:55
回答 1查看 89关注 0票数 2

我是C语言的新手,我想这样做:

代码语言:javascript
复制
void init(int array[][variable]);

编译器报告此错误:

代码语言:javascript
复制
error: expected expression

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

EN

回答 1

Stack Overflow用户

发布于 2017-12-10 08:54:29

你可以这样做:

void init1(int variable_nr_of_columns, int *array);或

void init2(int variable_nr_of_columns, int array[][variable_nr_of_columns]);

这是一个简单的程序来演示用法:

代码语言:javascript
复制
#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;
}

输出:

代码语言:javascript
复制
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 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47734805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档