'c++ "push_back" and "pop_back" for array passed by pointer

For context, this for a school assignment. I will attach a picture of the whole question, but to summarize the assignment, we have to make a RECURSIVE maze solver that will return the length of the path to solve the maze from start to finish, but we also have to input the path into an array, which is passed to the array by pointer. As far as I know, I cannot find the size of the array by pointer, and cannot figure out how to put the path into the array.

The question:

Question

Function we have to fill:

int runMaze(Maze& theMaze, int path[], int startCell, int endCell){}

I believe I am properly traversing the maze via DFS and returning the right path length, but I am not sure how to properly input/remove values from the path[].

Is there any way I can know the size of the path[] and be able to push_back() and pop_back() its elements?

This is an example of how our functions are called:

bool test1(std::string& error) {
    Maze theMaze("maze1.txt");
    int path[10];
    int pathLength = runMaze(theMaze, path, 0, 17);
    int correctLength = 10;
    int correct[10] = { 0, 1, 7, 8, 2, 3, 4, 5, 11, 17 };
    bool rc = true;
    if (!checkPath(path, pathLength, correct, correctLength)) {
        rc = false;
        if (pathLength != correctLength) {
            error = "Error 1a";
            error += ": runMaze() returned ";
            error += std::to_string(pathLength);
            error += ".  It should have returned ";
            error += std::to_string(correctLength);
        }
        else {
            error = "Error 1b: runMaze() does not create the correct path\n";
            error += "To see what is happening load the corresponding\n";
            error += "test and test1path.txt file at: \n";
            error += "https://seneca-dsa555-f21.github.io/dsa555-f21/\n";
        }
    }
    printPath("test1path.txt", path, pathLength, 3, 6);
    return rc;

}

There are a total of 10 tests it must pass.



Solution 1:[1]

Your questions:

Is there any way I can know the size of the path[]

No, there is absolutely no way you can determine the size of a raw array being passed to you without some sort of pre-determined terminator, such as \0 for character arrays.

A raw array as a parameter to a function end up just being a pointer to type. So your parameter is really just int* path, there is no size information included with this.

and be able to push_back() and pop_back() its elements?

There is no push_back() or pop_back() for raw arrays.

I am not sure how to properly input/remove values from the path[]

path[0] = 1;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Taekahn