Click here to Skip to main content
15,606,032 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
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:

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 << line;

			while (getline(isn,word,'\t')) {
				if(j==(66)) {break;}
			j=0; i++;

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.

1 solution

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:
class Matrix
    //number of rows of this matrix
    int Rows() const { return m_rows; }
    //number of columns of this matrix
    int Columns() const { return m_columns; }
    //direct access to the internal data: use with caution!!
    double* Data() { return m_data; }

    //constructs an empty matrix
        m_data = NULL;
        m_rows = 0;
        m_columns = 0;
    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
    //allocates memory for this matrix
    void Alloc(int rows, int columns)
        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;
    //reads the k-th element
    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]; }

    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:
template<int SIZE>
void func(double (*rot)[SIZE])

But I still think the Matrix class is a better solution.
Share this answer
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.
I will update my answer.
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

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