Solved

Passing a 2 dimensional array as a function parameter

Posted on 2002-06-05
8
185 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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.

821 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