0

I have an issue that I cannot seem to find the answer to other than perhaps a few "its not possible's" but it absolutely has to be possible so I figured I'd try to explain.

Essentially I have a problem where I read in a number of iterations A from a file then sizes x and y. In a function I pass x and y like so:

//Really bad pseudocode
main(){
   int i;
   ifstream input;
   input.open("somefile")
   input >> A;
   input >> x;
   input >> y;
   for(i = 0; i < A; i++){    
       somefunc(x,y);
   }
...}
somefunc(int x, int y){
    int matrix[x][y];
    ...
    someotherfunc(matrix);
...}
someotherfunc(int matrix[][]){
    ...
    someotherfunc(matrix[index][otherindex]);
...}

So my question is this, how can I make the call to "someotherfunc" above work. I use x and y as the size of the variable sizes for the matrix. Those change per iteration A. On one run it may be made as int matrix[1][10]. then on another maybe matrix[100][999] or matrix[999][1235415] and so on.

What kind of trick, I don't care how "bad practice" it is, can make this work? I have tried circumventing using a matrix that has an int** member that gets dynamically allocated in "somefunc" but assignment to it won't work for whatever reason. I need to be able to define a multidimensional array (matrix) and pass it between functions, particularly in a recursive manner. I have been on this for hours and have made no headway.

Before it is mentioned, I am quite well aware I left out checks for whether the file was successfully opened, I purposely did so as it has absolutely nothing to do with my issue.

I have found really mixed answers to this question but surely there has to be some solution.

Further information on this is I have no way of knowing ahead of time what the max X or Y will be that gets input.

I suppose I could use constant globals like 2147483647 (max value of a signed int) and initialize the matrix like 'int matrix[2147483647][2147483647]' but that seems like an almost obscene waste of space and surely there has to be a better way to do this.

  • 1
    You can't as such. This is a duplicate of: http://stackoverflow.com/questions/3911400/how-to-pass-2d-array-matrix-in-a-function-in-c – teroi Nov 14 '16 at 06:20
  • What is the output of your program? – Papipone Nov 14 '16 at 06:20
  • 1
    write, adapt, or adopt, a matrix class and pass that around instead. Here's a quick and relatively robust one: https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op. You may want to replace `double* data_;` with `vector` because it allows you to eliminate the destructor, copy constructor, assignment operator, and a bunch of extra complexity. very small performance hit on initialization because it'll default initialize all of the elements in the `vector`. – user4581301 Nov 14 '16 at 06:48
  • It occurs that you can also use `std::vector> matrix(x, std::vector(y));`, but I have a pathological hatred of `vector`s of `vector`s that I should probably see a doctor about. It's not normal. – user4581301 Nov 14 '16 at 06:52

0 Answers0