15,939,794 members
See more:
Hey guys, I had a quick question. I'm am trying to take a bunch of code I wrote and then copy/pasted 10 times.. and turn it into a function. Passing 2D arrays and pointers is still something I'm struggled with. I have functions that pass square 2D arrays in the following way:

C#
```void createTrans(double orig[3], double (*rot)[3], double (*transform)[4]){
for (int i=0;i<3;i++){
for (int j=0;j<3;j++){
transform[i][j] = rot[i][j];
if (j==2) {
transform[i][j+1] = orig[i];
}
}
}
transform[3][3]=1; transform[3][0]=transform[3][1]=transform[3][2]=0;
}```

Now here is the code I am trying to make into a function:

```double LG[100][66];

ifstream LGFile("mtlength\\LG.txt", ios::in);
if (LGFile.is_open())
{
stringstream iss, isn;
string line, word;

i=0; j=0;

while (getline(LGFile,line))
{
isn.clear();
isn << line;

while (getline(isn,word,'\t')) {
if(j==(66)) {break;}
LG[i][j]=atof(word.c_str());
j++;
}
j=0; i++;
isn.clear();
}
}
LGFile.close();```

So my question is fairly simple. How do I pass my LG matrix (of n x m) to a function as a pointer where it can be filled via the text file. Maybe I am just assuming that the `double (*rot)[3]` declaration specifies a 3x3 but it doesn't. Any help on this would be much appreciated.
Posted

## Solution 1

If you don't know the size of your matrix at compile time, then you can't.

Instead of dealing with 2D arrays, I suggest you declare a simple Matrix class:
C++
```class Matrix
{
public:
//number of rows of this matrix
int Rows() const { return m_rows; }
//number of columns of this matrix
int Columns() const { return m_columns; }
double* Data() { return m_data; }

//constructs an empty matrix
Matrix()
{
m_data = NULL;
m_rows = 0;
m_columns = 0;
}
//copy-constructor
Matrix(const Matrix& mat)
{
//clones the matrix if it is not empty
if (mat.Rows() != 0 && mat.Columns() != 0)
{
Alloc(mat.Rows(), mat.Columns());
memcpy(m_data, mat.Data(), m_rows * m_columns * sizeof(double));
}
}
//cleans this matrix
~Matrix()
{
Free();
}
//allocates memory for this matrix
void Alloc(int rows, int columns)
{
Free();
m_data = new[rows * columns];
m_rows = rows;
m_columns = columns;
}
//frees the memory
void Free()
{
if (m_data != NULL)
delete [] m_data;
m_data = NULL;
m_rows = 0;
m_columns = 0;
}
double operator[](int k) const { return m_data[k]; }
//writes the k-th element
double& operator[](int k) { return m_values[k]; }
//reads the element at line i and column j
double operator()(int i, int j) const { return m_data[i * m_columns + j]; }
//writes the element at line i and column j
double& operator()(int i, int j) { return m_data[i * m_columns + j]; }

private:
int m_rows, m_columns;
double* m_data;
};```

Then, don't forget to pass it by reference (or pointers) to any function, otherwise the copy-constructor will slow down the whole process. Or you can just remove the copy-constructor implementation (but also the destructor to avoid deleting the same pointer twice!)

-----------------

You can also use templates:
C++
```template<int SIZE>
void func(double (*rot)[SIZE])
{
}```

But I still think the Matrix class is a better solution.

v4
Donald Hume 6-Apr-11 12:52pm
I do know the size at compile time! How would I pass it then?
Olivier Levrey 6-Apr-11 13:49pm
Mmmm yes sorry. You could use templates then. But I think the best solution is still using a class.
Guyverthree 6-Apr-11 12:54pm
I agree, that is the simplest way to do what you want to do.
Sandeep Mewara 6-Apr-11 14:27pm
My 5!
Olivier Levrey 7-Apr-11 3:23am
Thank you Sandeep.

Top Experts
Last 24hrsThis month
 merano99 100 OriginalGriff 50 Graeme_Grant 45 Richard Deeming 40 Member 16307629 -4
 OriginalGriff 790 Pete O'Hanlon 725 Richard Deeming 690 merano99 335 Dave Kreskowiak 215

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900