2017-12-10 10 views
2

は、私はCに新たなんだと私はこれをしたい:私は、行の固定数が、ととの2次元配列を渡すにはどうすればよい可変数の列を持つ2次元配列を関数に渡すにはどうすればよいですか?

error: expected expression 

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

コンパイラはこのエラーを報告します関数に列の可変数?

+0

あなたの配列には、固定サイズの列と可変サイズの行がありますか? – Fureeish

答えて

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; 
} 

はOUTPUT:

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 
+0

* listは何ですか? @ sd7 –

+0

@jamesjake 'int * list'はポインタ'&list [0] [0] 'です – sg7

関連する問題