1

I am trying to pass a 2D array to a function that I have defined in my program, but for some reason, I am continuously getting an error about conflicting types for the function call and definition. I am not entirely sure what could be causing the issue since I have not only included the right header, but also used the proper parameter type in the function indicating that it is a 2D array. Below is the code that I am writing.

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

And this is the error that I receive after running the program:

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

It's really confusing me and after looking at previous posts on StackOverflow I cannot seem to find what could be causing this issue. I know this problem could possibly be solved if I converted the array into a pointer, but is it not possible to pass 2D arrays to function? If anyone could help me find a way to solve this, I'd truly appreciate it. Thank you.

1
  • 1
    Turn on all compiler warnings... Commented Oct 23, 2021 at 23:39

1 Answer 1

3

You need to define it before the main function or put there the function prototype

bool testFunction(char array[][6], int i, int j);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that seemed to fix the issue, but why does defining it before main() fix the issue? Why does the order of where functions are defined impact the output?
@StriveForGreatness Yes, you must always declare something before using it. This is logical, after all the code can't use something it doesn't know exists yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.