Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing a 2 dimensional array as a function parameter

Posted on 2002-06-05
8
Medium Priority
?
193 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
ID: 7058206
hmm...  What about using std vector?  Good luck...
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 800 total points
ID: 7058471
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
ID: 7058493
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:thienpnguyen
ID: 7058602
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
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 7058612
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
ID: 7058633
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
ID: 7059879
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
ID: 7060004
>> 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

885 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