Message: Re: How to return multi-dim arrays? Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

None Re: How to return multi-dim arrays? 

Keywords: multidimensional array, return value, member function, type definition
Forum: Analysis
Re: Question How to return multi-dim arrays? (Matthias Boehm)
Re: None Re: How to return multi-dim arrays? (John Allison)
Re: None Re: How to return multi-dim arrays? (John Allison)
Date: 14 Jan, 2005
From: Jeremy McCormick <Jeremy McCormick>

Hi.

I second John's idea of using vectors instead of multi-dimensional arrays.

It will save you headaches in the future and is definitely more "C++ like".

That said, I believe that the following code snippet illustrates what 
you would like to do using C-style multidimensional arrays.

--

#include <iostream>

// declare DblArr3 type
typedef double (*DblArr3)[1][1];

// returns pointer to a 3D array
DblArr3 ret_array()
{
        DblArr3 d = new double[1][1][1];
        d[1][1][1] = 1.0;
        return d;
}

// get pointer to new 3D array and print member at 1,1,1
int main(int argc, char**argv)
{
        DblArr3 d = ret_array();
        std::cout << d[1][1][1] << std::endl;
        delete [] d;
        return 0;
}

--

If you just want to pass a pointer to your array around,
then ret_array() would return some predefined array rather
than creating a new one.  And you would not call delete in 
main().

BTW, using raw pointers to your low-level data structures is generally 
a bad idea unless you absolutely cannot avoid it.

Good luck.

Inline Depth:
 1 1
 All All
Outline Depth:
 1 1
 2 2
 All All
Add message: (add)

 Add Message Add Message
to: "Re: How to return multi-dim arrays?"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews


[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]