繁体   English   中英

将二维数组传递给 C 中的函数时发生冲突类型错误

[英]Conflicting Types Error when Passing 2D Array to Function in C

我试图将一个二维数组传递给我在程序中定义的函数,但由于某种原因,我不断收到有关函数调用和定义类型冲突的错误。 我不完全确定是什么导致了这个问题,因为我不仅包含了正确的标题,而且还在函数中使用了正确的参数类型,表明它是一个二维数组。 下面是我正在编写的代码。

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

int main()
{
    char array[6][6] = {
        {'.', '.', '.', '.', '.', '.'},
        {'.', '.', '.','.', '.', '.'},
        {'.', '.', '.', '.', '.', '.'},
        {'.', '.', '.', '.', '.', '.'},
        {'.', '.', '.', '.', '.', '.'},
        {'.', '.', '.', '.', '.', '.'}
    };
    
    bool success = testFunction(array, 0, 1);

    return 0;
}


bool testFunction(char array[][6], int i, int j){
    return true;
}

这是我运行程序后收到的错误:

main.c:21:6: error: conflicting types for ‘testFunction’
   21 | bool testFunction(char array[][6], int i, int j){

这真的让我感到困惑,在查看了 StackOverflow 上以前的帖子后,我似乎无法找到可能导致此问题的原因。 我知道如果我将数组转换为指针,这个问题可能会得到解决,但是不能将二维数组传递给函数吗? 如果有人能帮我找到解决这个问题的方法,我将不胜感激。 谢谢你。

您需要在主函数之前定义它或将函数原型放在那里

bool testFunction(char array[][6], int i, int j);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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