【问题标题】:C - Can I set the size of a multidimensional array in a function parameter list to something higher than its actual size?C - 我可以将函数参数列表中的多维数组的大小设置为高于其实际大小吗?
【发布时间】:2020-04-27 11:12:37
【问题描述】:

如您所知,在 C 函数声明中,您必须命名除数组参数之一之外的所有维度大小。我现在正在使用一个二维数组,其中两个大小都是在运行时生成的,但是我想将该数组传递给另一个函数来填充它。

我的想法只是在函数声明中使用最大大小,如下所示:

int max_size = 100;
int actual_size;
int another_size;

static void fill_array(int ar[][max_size])
{
    for(int i = 0; i < another_size; i++)
    {
        for(int j = 0; j < actual_size; j++)
        {
            ar[i][j] = some int;
        }
    }
}

static void main()
{
    if(some_input) actual_size = 50;
    else actual_size = 100;

    if(some_input) another_size = 10;
    else another_size = 20;

    int empty_array[another_size][actual_size] = {0};
    fill_array(empty_array);
}

我的想法是,即使函数可能认为每个数组行有 100 个整数,我们也只是填充了前 50 个整数。这是笨手笨脚吗?有什么方法可以实现同样的更清洁?对 C 来说很新,如果这是一个非常明显的事情,很抱歉。

【问题讨论】:

  • 这很“不聪明”,因为为此目的使用全局变量并不是一个好习惯。只需将尺寸作为参数传递给函数。
  • 将 VLA 的维度作为函数参数传递。或者使用一维数组并自己计算索引。
  • 这能回答你的问题吗? Ways to pass 2D Array to function in C
  • 这只是其中之一。请在 Stackoverflow 中搜索“将 2d 数组传递给函数”,您将获得比您阅读更多的点击量。

标签: c arrays multidimensional-array variable-length-array


【解决方案1】:

首先要声明 main 函数

static void main()

不是标准的。

函数应该像这样声明

int main( void )

如果你的编译器支持变长数组,那么你可以像这样声明函数

static void fill_array( size_t rows, size_t cols, int ar[][cols] );

并传递任意大小的二维数组。

这是一个演示程序。

#include <stdio.h>

static void fill_array( size_t rows, size_t cols, int a[][cols] )
{
    for ( size_t i = 0; i < rows; i++ )
    {
        for ( size_t j = 0; j < cols; j++ )
        {
            a[i][j] = i * cols + j;
        }
    }
}

int main(void) 
{
    size_t rows;
    size_t cols;

    printf( "Enter the number of rows: " );
    scanf( "%zu", &rows );

    printf( "Enter the number of columns: " );
    scanf( "%zu", &cols );

    int a[rows][cols];

    fill_array( rows, cols, a );

    for ( size_t i = 0; i < rows; i++ )
    {
        for ( size_t j = 0; j < cols; j++ )
        {
            printf( "%2d ", a[i][j] );
        }
        putchar( '\n' );
    }

    return 0;
}

它的输出可能看起来像

Enter the number of rows: 3
Enter the number of columns: 5
 0  1  2  3  4 
 5  6  7  8  9 
10 11 12 13 14 

【讨论】:

    【解决方案2】:

    如果您想在函数中使用传递的数组作为“普通”数组,您可以使用此方法。甚至适用于 ANSI C。

    https://godbolt.org/z/aiNjef

    void fillarray(void *arr, int cols, int rows)
    {
        int (*newarr)[cols] = arr;
    
        for(int row = 0; row < rows; row++)
        {
            for(int col = 0; col < cols; col++)
            {
                newarr[row][col] = (row << 4) | (col);
            }
        }
    }
    
    #define ROWS    5
    #define COLS    10
    
    int main(void)
    {
        int arr[ROWS][COLS];
    
        fillarray(arr, COLS, ROWS);
        for(int row = 0; row < ROWS; row++)
        {
            for(int col = 0; col < COLS; col++)
            {
                printf("%02x ", arr[row][col]);
            }
            printf("\n");
        }
    }
    

    【讨论】:

    • 如果编译器不支持 VLA 那么这个声明 int (*newarr)[cols] = arr;无效。
    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多