1

So as you know, in C function declarations, you have to name all dimension sizes but one of array parameters. I'm now working with a 2d array where both sizes are generated at runtime, however I want to pass that array to another function to fill it.

My thought was just using the maximum size in the function declaration, like this:

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

My thought is that even though the function may think that each array line has 100 ints, we're only filling the first 50 anyways. Is this unclever? Any way to accomplish the same more cleaner? Pretty new to C, so sorry if it's a very obvious thing.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Noel93
  • 135
  • 11
  • 1
    It's "unclever" because it is not good practice to use global variables for this purpose. Just pass in the dimensions to the function as arguments. – kaylum Jan 09 '20 at 19:33
  • Pass the dimension of a VLA as a function argument. Or use a one-dimensional array and work out the indexing yourself. – Weather Vane Jan 09 '20 at 19:35
  • 1
    Does this answer your question? [Ways to pass 2D Array to function in C](https://stackoverflow.com/questions/40522341/ways-to-pass-2d-array-to-function-in-c) – kaylum Jan 09 '20 at 19:40
  • That's just one of the dups. Please search Stackoverflow for "pass 2d array to function" and you'll get more hits than you can read. – kaylum Jan 09 '20 at 19:41

2 Answers2

3

For starters such a declaration of the function main

static void main()

is not standard.

The function should be declared like

int main( void )

If your compiler supports variable length arrays then you may declare the function like

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

and pass a two-dimensional array of any sizes.

Here is a demonstrative program.

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

Its output might look for example like

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 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If you want to use passed array as a "normal" array in the function you can use this method. Works even with the 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");
    }
}
0___________
  • 60,014
  • 4
  • 34
  • 74