Solved

Passing a 2 dimensional array as a function parameter

Posted on 2002-06-05
8
184 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 200 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

786 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