home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,952 of 33,346   
   alexo to All   
   Re: Passing multi-dimensional arrays to    
   28 Mar 13 15:20:10   
   
   From: alelvb@inwind.it   
      
   > Varieble sized array are hard to handle even for compilators.   
      
   If you need a function more flexible you should use pointers   
   instead of arrays:   
   here rows and cols can be inputed at run-time and the function   
   generically manages every possible case.   
   This code for brevity doesn't check whether xp is a null pointer.   
      
   #include    
      
   using std::cout;   
   using std::endl;   
      
   void doublePointer(int **xp, int rows, int cols)   
   {   
        for(int ii = 0; ii < rows; ++ii)   
        {   
            for (int jj = 0; jj < cols; ++jj)   
            {   
                (*xp)[ii * rows + jj] = jj+1;   
                (jj % (cols - 1) == 0) && (jj != 0) ? cout <<   
                (*xp)[ii*rows + jj]   
                << endl : cout << (*xp)[ii*rows + jj];   
            }   
        }   
   }   
      
   int main(int argc, char** argv)   
   {   
      
       int rows = 5;   
       int cols = 3;   
      
       int *xarray = 0; // always initialize a pointer to 0   
      
       xarray = new int[rows*cols];   
      
       doublePointer(&xarray, rows, cols);   
      
       return 0;   
   }   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca