Home > Blockchain >  Adding a char matrix as a function argument throws syntax error
Adding a char matrix as a function argument throws syntax error

Time:06-06

I have a function that validates whether certain coordinates are within a matrix and returns true/false depending on the answer:

bool validateNextLocation(char robot, int proposed_row, int proposed_col, char map[7][7]){
    auto const robot_location = World::getRobotLocation(robot);
    int row = robot_location -> first;
    int col = robot_location -> second;

    if (map[proposed_row][col] != '1' || map[row][proposed_col] != '1'){return false;}
    else{return true;}

}

I am trying to use the function in my switch cases:

switch (direction) {
        case 'L': {
            if (World::validateNextLocation(robot,   robot_location->first, robot_location-> second, char a[7][7])){
                  robot_location->first;
            }
            else{return -1;}
        }
        break;
        case 'D': {
            if (World::validateNextLocation(robot, robot_location->first, --robot_location->second, char a[7][7])){
                --robot_location->second;
            }
            else{return -1;}
        }
        break;
        case 'R': {
            if (World::validateNextLocation(robot, --robot_location->first, robot_location->second, char a[7][7])){
                --robot_location->first;
            }
            else{return -1;}
        }
        break;
        default: {
            if (World::validateNextLocation(robot, robot_location->first,   robot_location->second, char a[7][7])){
                  robot_location->second;
            }
            else{return -1;}
        }
        break;
    }

But the char a[7][7] has a red underline where the error reads:

Expected '(' for function style cast or type construction

I know I'm not missing a bracket but where am I going wrong?

CodePudding user response:

Just change

if (World::validateNextLocation(robot, robot_location->first, 
      robot_location->second, char a[7][7])){

to

if (World::validateNextLocation(robot, robot_location->first, 
      robot_location->second, a)){

Declaring an array, and using an array are two different things, you don't use the same syntax for both. I am assuming that somewhere in your code you do have a proper declaration for a.

That said passing a 7x7 matrix from one function to another seems unlikely to be the right thing to do, but no doubt that will sort itself out in time.

CodePudding user response:

if (World::validateNextLocation(robot,   robot_location->first, robot_location-> second, char a[7][7])){

The char a[7][7] would declare a new variable a as 7x7 matrix.

First you can't declare a variable inside of function arguments, second the variable would uninitialized and thrid expire at the end of the function call. So three reasons this syntax makes no sense.

You have to declare your array before the function call if you don't have it already and then just pass the variable a as argument.

  •  Tags:  
  • c
  • Related