Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I declared an array z[m][n], m=100,n is an input variable from the dialog box, I use this method:
C++
double **z = new double *[m];
for (i=0;i<m;i++)
{
   z[i]=new double [n];
}
///////////
///////////

for (i=0;i<m;i++)
{
   delete []z[i];
}
delete []z;

I want to pass z[m][n] by
C++
func(z)
how can I write "func()"
it is wrong to declare as below:
C++
void func(double z[][n])

it is also wrong to declare as:
C++
void fun(double **z)
how can I solve the problem, thank you for your advice.
Posted

You can use either of the following forms:
C++
void func(double z[][])
{
}

void func(double **z)
{
}

The only issue for your function is how it determines the number of elements in each dimension of the array, so you would need to add some marker element in each level, or send some other parameters giving the dimensions.

If you are talking of sending a specific element to the function then it would be something like:
C++
void func(double d)
{
}

// called by
    func(z[x][y]);
 
Share this answer
 
v2
Comments
Angela2012 30-Jun-12 4:07am    
thank you for your help, I am still a little confused,how does the complier allocate room for array z[m][n] if we use
void func(double z[][])
{
}

void func(double **z)
{
}
Richard MacCutchan 30-Jun-12 4:21am    
The function definition just tells the compiler that the parameter being passed in (z) is a pointer to a multi-dimensional array (either format means the same thing). Then, within the function you need some extra information to tell how long each dimension of the array actually is. So you would need to use a fixed length array, or send the values of m and n as extra parameters to the function.
Angela2012 30-Jun-12 9:14am    
Thank you.
Richard MacCutchan 30-Jun-12 9:18am    
However, as usual I think Aescleal's advice offers a much cleaner way of doing it. Learning STL is a good idea if you are going to be doing a lot of development in C++.
Don't use raw arrays in C++, it's just not worth the pain. Use a vector of vectors, they'll be as fast and far easier to pass around your program. You don't have to mess around with manual memory management either. You can initialise them in virtually the same way as you do in your code:
C++
std::vector<std::vector<double>> z( m );
std::for_each( begin( z ), end( z ) [n]( std::vector<double> &v )
{
    v.resize( n );
} );
or be a lot more direct but with a bit more copying:
C++
std::vector<double> y( n );
++>std::vector<std::vector<double>>; z( m, y );
When you want to pass the data to another function you declare the function as:
C++
void do_something( std::vector<std::vector<double>> &v )
{
    // Damage to taste in here
}
and you don't have to worry about the size of the data you pass around from pillar to post as you can always ask the vectors. If you use "real" arrays you have a problem in that the "arrayness" of the objects decays to nothing more than a pointer to pointer to double, which complicates the function as you have to either make assumptions about the size of the data OR pass the size of both dimensions into the function.

So generally if you have to use arrays for anything you're probably doing something wrong unless you're interacting with the OS or hardware.
 
Share this answer
 
v2
Comments
Angela2012 30-Jun-12 9:10am    
Thank you, I will learn to use it
Aescleal 30-Jun-12 13:39pm    
Using the standard library isn't an optional extra in C++. Unfortunately most lecturers and textbooks are idiots/written by idiots and they think that you have to learn all the unsafe bits should be taught first as they're somehow more fundamental. If they didn't tell you about arrays and pointers you could get confident with the language before banging your head against them.

Anyway, rant over, good luck in whatever you're using this code for.
Richard MacCutchan 30-Jun-12 9:17am    
Sage advice as usual; I must learn to think in STL. :(
Aescleal 30-Jun-12 13:35pm    
It's not so much "learning to think in STL" as getting your head around the standard library. A good way in is to say: "Okay, I'm not writing another loop." Even if you fail you'll start to get a handle on it.

Of course I wish my co-workers in the games industry thought the same way.
Try it :) :
C++
class CDblMtxPar
{
  double** m_ppData;
  int m_iRows;
  int m_iCols;

public:
  CDblMtxPar(double** ppData, int iRows, int iCols)
    : m_ppData(ppData), m_iRows(iRows), m_iCols(iCols) {}

  double** GetData() const { return m_ppData; }
  int GetRowsCount() const { return m_iRows; }
  int GetColsCount() const { return m_iCols; }
};

void yourFunction(const CDblMtxPar& cPar)
{
  // use the dimensions and data here
}

void test()
{
  // double** z, int m, int n
  //..

  CDblMtxPar cPar(z, m, n);
  yourFunction(cPar);
}
 
Share this answer
 
v2
Comments
Angela2012 30-Jun-12 9:10am    
Thank you.

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