Solved

Passing a 2 dimensional array as a function parameter

Posted on 2002-06-05
8
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

632 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