C++ 2D array to function when size is variable

I need to pass 2D array of struct to function.

struct Point {  
  int x, y;
  double f, g, h;
  int parentX, parentY;
  int status;

int main(int argc, char* argv[])
  Point grid[fieldX][fieldY];
  void something(grid){}

How to?

1 answer

  • answered 2018-11-08 07:35 Fantastic Mr Fox

    You have plenty of choices:

    Use std::vector this makes it super easy:

    std::vector<std::vector<Point>> Grid;
    void something(std::vector<std::vector<Point>> &grid) { ...

    If you know the size is going to be set at compile time always, use a std::array:

    typedef std::array<std::array<Point, YDimSize>, XDimSize> Grid;
    Grid grid;
    void something(Grid &grid) { ...