passing 2d array created using <vector> --simple example

Hello,
I am trying to pass a 2D array called f (coming from a text file with 9 columns of numbers and 297,680 rows) that was created using the vector container in my main() to the function myfunc. I'm just trying to figure out how to pass the address of f in main() to myfunc(), so that myfunc() has arguments consisting of a pointer g (that accepts the address of f as an input) and an int.
This is the error from the compiler:
test_2d.cc: In function ‘int main()’:
test_2d.cc:47:25: error: cannot convert ‘std::vector<std::vector<double> >*’ to ‘double (*)[297680][9]’ for argument ‘1’ to ‘int myfunc(double (*)[297680][9], int)’
return myfunc(&f,count);
^

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <iomanip> //allow setprecision to get all the decimal points
#include <vector>
#include <string>
#include <cmath>

using namespace std;

int myfunc(double (*g)[297680][9], int newcount)
{
 for(int i=0; i<newcount; i++)
  {
     for (int j=0; j<9; j++)
     {
	cout << *g[i][j] << "\t"; //check our array
     }
     cout << endl;
  }
  cout << "function called correctly " << endl; 
  return 0; 
}

int main() 
{
  int numRows=297680;
  int numCols=9;
  int count=0;
  ifstream myfile("Shear_xy_100_dist.dump");
  vector<vector<double> > f; //initialize with no size!
  for (int i=0; i<numRows; i++)
  {
    vector<double> row; // Create an empty row
    for (int j=0; j<numCols; j++) //for the row
    {
	double temp;
	myfile >> temp;
	row.push_back(temp); // Add an element to the row
    }
	if(row.at(1)==1)
	{	
	  count = count+1;
	  f.push_back(row); // Add the whole row vec to the main vector
	}
  }

  return myfunc(&f,count);  
}
Why a pointer to (arrays) of double? Why wouldn't your function use a vector?
Also, a vector of vector of foo doesn't have the same storage as a 2D array. The storage is continuous, but the outermost vector is an array of vector objects, which store their data elsewhere on the heap.

Also, line 16 doesn't need that asterisk.

1
2
3
4
5
6
7
8
9
10
11
int myfunc( vector<vector<double>> & g )
{
  for (size_t i=0; i<g.size(); i++)
  {
    for (size_t j=0; j<g[i].size(); j++)
    {
      cout << g[i][j] << "\t";
    }
    cout << endl;
  }
}

If you really want to use C-style arrays, here is some more reading on handling them:
http://www.cplusplus.com/forum/beginner/120441/#msg655547
http://www.cplusplus.com/forum/general/49079/#msg266703

Hope this helps.
Thanks Duoas, the code compiles and runs correctly with your suggestion, provided that these are the arguments of myfunc(f,count) when called in main(). The scoping is such that any changes to g made in myfunc() affect f in main().

One thing that bugs me, it seems like it should be myfunc(&f,count) but that does not work. I feel like I want to pass the address (not the value) to the function which knows it should be receiving an address, namely vector<vector<double> > &g, the address of some dynamic array g, right? Why f and not &f when I call myfunc() in main()?

I think the c++ syntax appears inconsistent with respect to pointers and references when dealing with arrays or vectors. When using pointers and references for single variables and passing either to functions, the logic and syntax (*, **, &, etc.) seem consistent and make sense. But I'm sure I just don't fully understand what's happening. I'll look at the references you suggest :)
I agree, actually -- in C++ it is hard to know whether you are passing a reference just by looking at the call.

But that doesn't matter -- it is what it is.

(There is actually a very good rationale: a reference is really an alias for the object -- it is possible that no pointers are involved at all. Though in this case it is most likely a veiled pointer. The point is, however, that the compiler can reason about it and do the Right Thing much more easily than it can with a straight-up pointer.)

If you really want to pass a pointer, you still can:

1
2
3
4
5
6
7
8
9
10
11
12
int myfunc( vector<vector<double>>* g )
{
  ...
}

int main()
{
  vector<vector<double>> f;
  ...
  myfunc( &f );
  return 0;
}

BTW, I just noticed that you are passing the returning the result of myfunc() from main(). Don't do that.

Hope this helps.
Got it. Thanks a lot!
Topic archived. No new replies allowed.