簡體   English   中英

將二維數組傳遞給 C 中的 function?

[英]Passing a 2D array to a function in C?

我需要將二維數組傳遞給 function。

#include <stdio.h>

#define DIMENSION1 (2)
#define DIMENSION2 (3)

void func(float *name[])
{
    for( int i=0;i<DIMENSION1;i++){
        for( int j=0;j<DIMENSION2;j++){
            float element = name[i][j];
            printf("name[%d][%d] = %.1f \n", i, j, element);
        }
    }
}


int main(int argc, char *argv[])
{
        float input_array[DIMENSION1][DIMENSION2] = 
        { 
            {0.0f, 0.1f, 0.2f}, 
            {1.0f, 1.1f, 1.2f} 
        };

        func(input_array);

        return 0;
}

尺寸因用例而異, func應保持不變。

我嘗試了上面的int func(float *[])但編譯器抱怨expected 'float **' but argument is of type 'float (*)[3]' ,並且在嘗試訪問時我在運行時遇到分段錯誤錯誤element = name[i][j]處的數組。

我的 function 的正確簽名是什么? 還是我需要以不同的方式調用func

您可以像這樣更改您的 function;

int func(float (*arr)[DIMENSION2])
{

}

但你也應該像這樣改變你的主要代碼;

float input[DIMENSION1][DIMENSION2];//I just upload the dimension1 to dimension2

如果您使用不同大小的 object,則應將其封裝在struct中。

struct array2D
{
  int rows, columns;
  float values[1];
};
typedef struct array2D array2D;

array2D * create2D( int rows, int columns )
{
  array2D * result = calloc( sizeof(array2D) + sizeof(float) * rows * columns - 1, 1 );
  if (result)
  {
    result->rows = rows;
    result->columns = columns;
  }
  return result;
}

float * index2D( array2D * a, int row, int column )
{
  return a->values + row * a->rows + column;
} 

等等。

void my_fn( array2D * A )
{
  *index2D( A, A->rows-1, A->columns-1 ) = -7;
}

int main()
{
  array2D * A = create2D( 3, 4 );
  *index2D( A, 1, 2 ) = 3.141592;
  my_fn( A );
  for (int row = 0; row < A->rows; row++)
  {
    for (int col = 0; col < A->columns; col++)
      printf( "%10.2f ", *index2D( A, row, col ) );
    printf( "\n" );
  }
  free( A );
}

編輯:使用這種結構,您甚至可以處理單個行:

void my_fn2( float * row, int n )
{
  row[n-1] = 5;
}

int main()
{
  ...
  my_fn2( index2D( A, 2, 0 ), A->columns );
  ...
}

您可以使用以下 function 原型:

int func(int dim1, int dim2, float array[dim1][dim2]);

為此,您必須將兩個維度都傳遞給 function(無論如何,您在函數中都需要此值)。 為了提高 function 調用的可用性,您可以使用以下宏:

#define FUNC_CALL_WITH_ARRAY(array) func(sizeof(array)/sizeof(*(array)), sizeof(*(array))/sizeof(**(array)), array)

然后您可以使用以下命令調用 function:

FUNC_CALL_WITH_ARRAY(input_array);

完整示例:

#include<stdio.h>
#include <stdlib.h>
#include <string.h>

#define FUNC_CALL_WITH_ARRAY(array) func(sizeof(array)/sizeof(*(array)), sizeof(*(array))/sizeof(**(array)), array)
int func(int dim1, int dim2, float array[dim1][dim2])
{
    printf("dim1 %d, dim2 %d\n", dim1, dim2);

    return 0;
}

#define DIMENSION1 (4)
#define DIMENSION2 (512)



int main(int argc, char *argv[])
{
    float input_array[DIMENSION1][DIMENSION2];

    FUNC_CALL_WITH_ARRAY(input_array);

    float input_array2[7][16];

    FUNC_CALL_WITH_ARRAY(input_array2);
}

將打印

dim1 4, dim2 512
dim1 7, dim2 16

尺寸因用例而異,功能應保持不變。

使用 VLA:

void func (int r, int c, float arr[r][c]) {
    //access it like this
    for (int i = 0; i < r; ++i) {
        for (int j = 0; j < c; ++j) {
            printf ("%f\n", arr[i][j]);
        }
    }
}

// call it like this

func (DIMENSION1, DIMENSION2, input_array);

如評論中所述,關鍵問題是int func(float *name[])name聲明為指向 float 的指針數組

從這個意義上說,對main()的以下修改有效:

int main(int argc, char *argv[])
{
        float input_array[DIMENSION1][DIMENSION2] = 
        { 
            {0.0f, 0.1f, 0.2f}, 
            {1.0f, 1.1f, 1.2f} 
        };

        /* Declare an array of pointers, as this is what func requires at input: */
        float* in_p[DIMENSION1];       
        /* ... and initialize this array to point to first elements of input array: */
        for( int i=0;i<DIMENSION1;i++)
            in_p[i] = input_array[i];
        /* ... and send this array of pointers to func: */
        func(in_p);

        return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM