1

I have 2 2D arrays that represent a maze

const char maze1[10][11] and const char maze2[20][21]

I'm trying to create 1 function to handle both mazes like so:

void solveMaze(maze[][])
{
}

and just pass the maze like solveMaze(maze1);
However, I have to supply a size for the array, which is different depending on which maze is being passed in. Without overloading the function or using function templates, how can I have 1 function to handle both arrays?

6
  • 1
    If you're writing C++, is there any particular reason you're not using STL containers? Commented Oct 10, 2011 at 4:50
  • not familiar with STL, I'm still a beginner Commented Oct 10, 2011 at 4:50
  • 1
    Also, aren't these 2D arrays? Commented Oct 10, 2011 at 4:51
  • 2
    I'm going to dispute the claim that you have two 3D arrays. Those look rather two-dimensional to me. And the answer is: you can't. You either need to pass in xMax and yMax as well or use STL containers (which is what you should be using) Commented Oct 10, 2011 at 4:54
  • 1
    @dukevin - see Brendan's answer below. A vector is one example which would be perfect here. EDIT: Also; cplusplus.com/reference/stl Commented Oct 10, 2011 at 4:57

3 Answers 3

7

C++ answer

Use std::vector:

// Initialize the vector with 11 rows of 10 characters
std::vector<std::vector<char> > maze(11, std::vector<char>(10));

void solveMaze(const std::vector<std::vector<char> > &maze) {
    // note that you can access an element as maze[x][y]
}

The boost::multi_array is slightly more efficient (if you're allowed to use boost). I think it goes something like this:

boost::multi_array<char, 2> maze(boost::extents[10][11]);

void solveMaze(const boost::multi_array<char, 2> &maze) {
    // note that you can access an element as maze[x][y]
}

C answer

Use pointers:

const char maze1[10][11];

void solveMaze(char *maze, size_t x_length, size_t y_length) {
    // note that you can access an element as maze[x + (x_length * y)]
}
Sign up to request clarification or add additional context in comments.

3 Comments

Your C example is way wrong. A 2D array in C is NOT an array of pointers.
@Dave - Right, sorry. I don't do C enough. It's fixed now.
For the C++ version, you should probably pass either a reference or a const reference to the std::vector. Otherwise, there will be an unnecessary (and perhaps incorrect) copy of the container.
1

Std c++ doesn't allow variably sized arrays. Gnu extensions allow this.

given a gnu compiler, you can

 void solvemaze(int w, int h, const char maze[h][w])
 {    //solve it...
 }

otherwise,

 void solvemaze(int w, int h, const char *maze)
 {    //solve it, bearing in mind:
      //maze[y][x] = maze[(w*y)+x];
 }

1 Comment

that gnu compiler answer looks really nice. if only i had gnu :(
1

Actually it can be solved without vector:

template<size_t N, size_t M>
void foo(char (&maze)[N][M])
{
    // do your stuff here
}

On the other hand, I would also prefer to use vectors: it just feels safer.

Comments

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.