Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

arrays - C++ argument of type "char" is incompatible with parameter of type "char(*)[7]"

I've followed steps that were all required so far for a school project and I am getting these two errors ( C++ argument of type "char" is incompatible with parameter of type "char(*)[7]" and 'void boardInit(char [][7])': cannot convert argument 1 from 'char[][7]' ) when trying to call my function to make sure the array is initializing properly. Can anyone tell me why I am getting these errors and how to fix them?

The code below is in a header file.

typedef char Board[BOARD_SIZE][BOARD_SIZE];

void boardInit(Board board);

Both the above definitions were required for the program and can not be changed.

The code below is in my Main.cpp file

int main()
{        
    Board board;
    
    boardInit(**board**[BOARD_SIZE][BOARD_SIZE]);
}

Above is where I am attempting to call the function (and failing) the errors are coming from the board enclosed in the double stars. (I do not have the stars in the program it's just to point out the problem.

Below is the function I'm looking to implement, it is in yet another file called Board.cpp.

void boardInit(Board board)
{
    for (int i = 0; i < BOARD_SIZE; ++i) 
    {
        for (int j = 0; j < BOARD_SIZE; ++j) 
        {            
            board[i][j] = 1;
        }
    }
}

I have all the necessary includes and I can not switch the file they are in because ALL of that information was specified by my professor.

EDIT: We were not provided with a textbook (nor was one even suggested for if we got stuck).

question from:https://stackoverflow.com/questions/65862347/c-argument-of-type-char-is-incompatible-with-parameter-of-type-char7

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a typedef that makes saying Board board equivalent to saying char board[BOARD_SIZE][BOARD_SIZE].

Imagine you didn't have the typedef, the function prototype would be: void boardInit(char board[BOARD_SIZE][BOARD_SIZE]);

If you had declared as char board[BOARD_SIZE][BOARD_SIZE], how would you call that function passing board? Just boardInit(board);. You dont have to specify the dimensions when calling the function, only when declaring/defining it!

int main()
{        
    Board board;
    
    boardInit(board);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...