Solved

Passing a 2 dimensional array as a function parameter

Posted on 2002-06-05
8
181 Views
Last Modified: 2010-04-02
I need to write a function that recieves a two dimensional array as a parameter. The problem is that the size of both dimensions are unknown, and will change each time the function is called. I started out writing it like this...

main()
{
   double lfArray[25][8];
   int nSizeDim1 = 25;
   int nSizeDim2 = 8;

   someFunction(lfArray, nSizeDim1, nSizeDim2);
}

void someFunction(double** lfArray, int nSizeDim1, int nSizeDim2)
{
     do something with the array;
}

Then I changed the calling function to look like...
   someFunction((double**)lfArray);

Using the cast to the double** the program compiled, but the data in the two dimensional array was not accessible from the someFunction.

Any help would be greatly appreciated.
Thanks
0
Comment
Question by:willmrk
8 Comments
 
LVL 1

Expert Comment

by:Toad224
Comment Utility
hmm...  What about using std vector?  Good luck...
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 200 total points
Comment Utility
One way to solve your problem is to allocate memory to "lfArray" dynamically.

double **lfArray;
int nSizeDim1 = 25;
int nSizeDim2 = 8;

lfArray=new double*[25];
for (int i=0; i<25; i++)
  lfArray[i]=new double[8];

Then you don't need casting when passing to function.
0
 
LVL 23

Expert Comment

by:Roshan Davis
Comment Utility
Calling

pszResult = new DWORD*[10];
pszResult[0] = new DWORD;
etc

someFunction((DWORD*&)pszResult)

//////////////////////////////////


someFunction(DWORD* &pBufferResult, int nSizeDim1, int nSizeDim2)
{
     DWORD **ppdwArray = (DWORD**)pBufferResult;

     ppdwArray[index][index];
     
     do something with the array;
}



GOOD LUCK
0
 
LVL 6

Expert Comment

by:thienpnguyen
Comment Utility
void someFunction(double** lfArray, int nSizeDim1, int nSizeDim2)
{
   for( int row=0; row< nSizeDim1; row++)
      for( int col =0; col < nSizeDim2; col++ )
         // element ( row, col ) = 0
         lfArray[ row*nSizeDim2 + col] = 0
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:thienpnguyen
Comment Utility
Sorry, fucntion prototype should be

void someFunction(double* lfArray, int nSizeDim1, int nSizeDim2);

double* lfArray; //not double** lfArray

void someFunction(double* lfArray, int nSizeDim1, int nSizeDim2)
{
  for( int row=0; row< nSizeDim1; row++)
     for( int col =0; col < nSizeDim2; col++ )
        // element ( row, col ) = 0
        lfArray[ row*nSizeDim2 + col] = 0
}

Note :  

Assume we have

double arr[SizeRow][SizeCol];

We wil have

&arr[row][col] = ((double *) arr) + row*SizeCol + col



0
 
LVL 6

Expert Comment

by:thienpnguyen
Comment Utility
Here is a example code

#include <iostream>
#include <iomanip>

using namespace std;

void someFunction(double* lfArray, int nSizeRow, int nSizeCol)
{
    double value = 0.;
    for( int row =0; row < nSizeRow; row++)
        for( int col =0; col < nSizeCol; col++)
        {
            lfArray[row*nSizeCol + col] = value;
            value = value+1;
        }

}


main()
{
  double lfArray[3][8];
  int nSizeDim1 = 3;
  int nSizeDim2 = 8;

  someFunction( (double *)lfArray, nSizeDim1, nSizeDim2);

    for( int row =0; row < nSizeDim1; row++)
    {
        for( int col =0; col < nSizeDim2; col++)
            cout << setw(3) << lfArray[row][col] << "  ";
        cout << endl;
    }
           

}


The output wil be

  0    1    2    3    4    5    6    7
  8    9   10   11   12   13   14   15
 16   17   18   19   20   21   22   23
0
 

Author Comment

by:willmrk
Comment Utility
Worked great.

I tried figuring out the vector approach, but couldnt find any info on two dimensional vectors. And I forgot to mention (and show in my example) that the two dimensional array would be full of data BEFORE passing to the function, so some of the other comments didnt apply.

Thanks to everyone for their assistance.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> I tried figuring out the vector approach,
I would strongly recommend a vector or other class for solving this problem.  The solutions suggested will certainly work, but are invitations for bugs.  If you never make mistakes, they are fine.  If you are human...I'd try something else.

You can easily create a 2D vector array by using a vector of vectors.  However, each of the vector's contained in the outer vector cna have different sizes.  i.e. the array may not be a "rectangle".  (This is true of the current solution too.)  However, sometimes this is actually disirable and often it is not a problem.

anyways you can do this like

void somefunction (const  vector< vector<int> > &V)
{
   for (int y = 0; y < V.size(); ++x)
   {
       for (int x = 0; x < V[y].size(); ++x)
            cout << V[y][x];
       cout << endl;
   }
};

Note that one common mistake is to not include a space between the >'s that apear in the nested tempalte declaration.   For example

const  vector< vector<int>> &V    // Error, needs a space.
const  vector< vector<int> > &V  // Okay.

the two >'s together  form a single >> operator, and according to the max-munch principle, must be parsed as a single operator, not two sperate ones.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now