简体   繁体   中英

Pass a 2D char array to a function in C

I'm a beginning programmer who is confused with passing a two dimensional array to a function. I think it may just be a simple syntax problem. I've looked for an answer, but nothing I've found seems to help, or is too far above my level for me to understand.

I identify the array and the function in the main function as, and after initializing it, attempt to call it:

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

int const ROWS = 8;
int const COLS = 8;

int main(int argc, char** argv) {

char board[ROWS][COLS];

bool canReach(char board[][], int i, int j);

//initialize array

//values of i and j given in a for loop

canReach(board, i, j);

return (EXIT_SUCCESS);
}

While writing the function outside the main function, I defined it exactly the same as I did in the main function.

bool canReach(char board[][], int i, int j){
//Functions purpose
}

When I attempt to build the program, I'm given this error twice and the program does not build:

error: array has incomplete element type 'char[][]'
bool canReach(char board[][], int i, int j)
                        ^

Please note that I'm trying to pass the entire array to the function, and not just a single value. What can I do to fix this problem? I would appreciate it if it didn't have to use pointers, as I find them quite confusing. Also, I've tried to leave out things that I thought weren't important, but I may have missed something I needed, or kept in things I didn't. Thank you for your time in helping out this starting programmer!

You can just pass arrays as function arguments with definition of their size.

bool canReach(char board[ROWS][COLS], int i, int j);

When the size is unknown, pointers are the way.

bool canReach(char* board, int i, int j);

You should know, that arrays != pointers but pointers can storage the address of an array.

Here is a demonstrative program

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

bool canReach( int n, int m, char board[][m] )
{
    for ( int i = 0; i < n; i++ )
    {
        for ( int j = 0; j < m; j++ )
        {
            board[i][j] = 0;
        }
    }

    return printf( "Hello SaarthakSaxena" );
}    

int main( void )
{
    const int ROWS = 8;
    const int COLS = 8;

    char board[ROWS][COLS];

    canReach( ROWS, COLS, board );

    return EXIT_SUCCESS;
}

Its output is

Hello SaarthakSaxena

Defining a function inside another function (here: main ) is not allowed in C. That is an extension of some compilers (eg gcc), but should not be used.

You have to specify the dimension of the array. C arrays do not have implicit size information as in HLL.

It also is not a good idea to use const variables for array dimensions in C. Instead

#define ROWS 8
#define COLS 8

Assuming i and j are the indexes of an element in the array, you can use the signature:

bool canReach(size_t rows, size_t cols, char board[rows][cols],
        size_t i, size_t j);

This allows to pass arrays of (run-time) variable size to the function. If the dimensions are guaranteed fixed at run-time:

bool canReach(char board[ROWS][COLS], size_t i, size_t j);

But only if using the macros above. It does not work with the const variables .

Both versions tell the compiler which dimension the array has, so it can calculate the address of each element. The first dimension might be omitted, but there is nothing gained and it would imhibit optional bounds checking (C11 option). Note the 1D-case char ca[] is just a special version of this general requirement you can always omit the left(/outer)most dimension.

Note I changed the types to the (unsigned) size_t as that is the appropriate type for array-indexing and will generate a conversion warning if properly enabled (strongly recommended). However, you can use int , but have to ensure no value becomes negative.

Hint: If you intend to store non-character integers in the array or do arithmetic on the elements, you should specify the signed-ness of the char types. char as such can be either unsigned or signed, depending on implementation. So use unsigned char or signed char , depending what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM