0

I'm trying to set an array as an input parameter to a self-made function, in which I want to modify the values of the array. For that, I tried to set the input array in the definition of the function as a pointer, but gave me some trouble.

The part of the *.hpp file can be seen here:

void CrossWall(int, int, bool[]);

The part of the *.cpp file is the next one:

void NODE::CrossWall(int robot_x, int robot_y, bool done_checking[]){

    if (((robot_x+1) > (current_map.CheckLength() - 1)) && !done_checking[3] ){
        available_movements[3] = 0;
        done_checking[3] = true;
    }
    if (((robot_x-1) < 0 ) && !done_checking[2]){
        available_movements[2] = 0;
        done_checking[2] = true;
    }
    if (((robot_y+1) > (current_map.CheckHeight() - 1)) && !done_checking[0]){
        available_movements[0] = 0;
        done_checking[0] = true;
    }
    if (((robot_y-1) < 0 ) && !done_checking[1]){
        available_movements[1] = 0;
        done_checking[1] = true;
    }
}

The array I want to modify is the array of bools (the only one there).

  • 1
    What kind of trouble? And is `available_movements` defined somewhere? – jkb Nov 20 '20 at 19:34
  • 2
    Code looks OK, what kind of trouble are you having? – john Nov 20 '20 at 19:38
  • 3
    You may want to consider std::array or std::vector for this – drescherjm Nov 20 '20 at 19:45
  • @jkb available_movements is defined as a class member of the class NODE. The trouble is that I want to modify the done_checking variable inside the function, but like that is won't do it because it isn't a pointer. –  Nov 20 '20 at 19:49
  • @john the same in my previous comment. –  Nov 20 '20 at 19:49
  • @pdaranda661 What makes you think it won't do it? Is the function actually failing to modify the array? – jkb Nov 20 '20 at 19:54
  • ***but like that is won't do it because it isn't a pointer*** You can change the value of the elements. You can't add additional elements or remove any. You don't even know how many elements this type of array has unless you pass that as a parameter. – drescherjm Nov 20 '20 at 19:58
  • 1
    I think you need to add some more code context. You need a [mcve] because your problem is not shown in the code that is presented or at least to me it's not at all obvious. – drescherjm Nov 20 '20 at 20:01
  • @jkb just did a test, and it actually modifies it. But I don't understand it. When I want to modify an input parameter in a function I need to introduce it as a pointer, but in this case, with an array, no. Why? –  Nov 20 '20 at 20:25
  • Does this answer your question? [What is array to pointer decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) – drescherjm Nov 20 '20 at 20:28
  • 1
    The pointer is what is passed by value, not the array of values. Quoting from the accepted answer of that question: ***If you're passing an array by value, what you're really doing is copying a pointer - a pointer to the array's first element is copied to the parameter (whose type should also be a pointer the array element's type). This works due to array's decaying nature; once decayed, sizeof no longer gives the complete array's size, because it essentially becomes a pointer.*** – drescherjm Nov 20 '20 at 20:29
  • This doesn’t address the question, but `if (((robot_x+1) > (current_map.CheckLength() - 1)) && !done_checking[3] )` can be written with far fewer parentheses. `if (robot_x+1 > current_map.CheckLength() - 1 && !done_checking[3] )` means exactly the same thing, without all the distractions. – Pete Becker Nov 20 '20 at 22:26

1 Answers1

0

I think I found a point of confusion:

won't do it because it isn't a pointer.

In fact, it IS a pointer: What is array to pointer decay?

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27