Re: [ROOT] passing table

From: cstrato (aon.912121399@aon.at)
Date: Sun Sep 07 2003 - 16:44:08 MEST


Dear Christian

Thank you for your extensive example explaining the three different
ways to create a table. I was afraid to hear that the second way will
not work.

Best regards
Christian

Christian Holm Christensen wrote:

>Hi Christian,
>
>cstrato <aon.912121399@aon.at> wrote concerning
>  [ROOT] passing table [Sat, 06 Sep 2003 21:08:22 +0200] 
>----------------------------------------------------------------------
>  
>
>>Sorry for this non root question:
>>I have a function:
>>   void MyFunction(Int_t n, Int_t m, Double_t **table)
>>
>>Usually I create the table on the heap an pass it to MyFunction().
>>However, when I create the table on the stack:
>>   Double_t table[4][8]
>>I am unable to pass it to MyFunction()
>>    
>>
>
>The conversion from `double(*)[]' to `double**' isn't legal.  In the
>first case, you have a specific memory layout, while in the latter, it
>is completely arbitrary.  
>
>  
>
>>you know how to pass the table to MyFunction()?
>>    
>>
>
>I would suggest using `calculated index', or perhaps `std::valarray'.
>For the former, see the attached file. 
>
>Yours, 
>
> ___  |  Christian Holm Christensen 
>  |_| |	 -------------------------------------------------------------
>    | |	 Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
>     _|	          DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
>    _|	          Denmark                    Office: (+45) 353  25 305
> ____|	 Email:   cholm@nbi.dk               Web:    www.nbi.dk/~cholm
> | |
>
>  
>
>------------------------------------------------------------------------
>
>#include <iostream>
>#include <iomanip>
>#include <vector>
>
>void function(double** array, size_t n, size_t m) 
>{
>  std::cout << "Array of size (" << n << "x" << m << "): " << std::endl;
>  for (size_t i = 0; i < n; ++i) {
>    for (size_t j = 0; j < m; ++j) 
>      std::cout << std::setw(3) << array[i][j] << std::flush;
>    std::cout << std::endl;
>  }
>} 
>
>void function(double* array, size_t n, size_t m) 
>{
>  std::cout << "Array of size (" << n << "x" << m << "): " << std::endl;
>  for (size_t i = 0; i < n; ++i) {
>    for (size_t j = 0; j < m; ++j) 
>      std::cout << std::setw(3) << array[i * n + j] << std::flush;
>    std::cout << std::endl;
>  }
>}
>
>int main() 
>{
>  double** a1 = new double*[3];
>  for (size_t i = 0; i < 3; ++i)  {
>    a1[i] = new double[2];
>    for (size_t j = 0; j < 2; ++j) 
>       a1[i][j] = i * 10 + j;
>  }
>
>  function(a1, 3, 2);
>
>  double a2[2][3];
>  for (size_t i = 0; i < 2; ++i) 
>    for (size_t j = 0; j < 3; ++j) 
>      a2[i][j] = 10 * j + i;
>
>  // function(&(a2[0]), 2, 3);
>
>  double a3[6];
>  for (size_t i = 0; i < 2; ++i) 
>    for (size_t j = 0; j < 3; ++j) 
>      a3[i * 2 + j] = 10 * j + i;
>
>  function(a3, 2, 3);
>
>  return 0;
>}
>
>  
>



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:15 MET