Thread: returning a double array [][]

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    6

    Question returning a double array [][]

    Is there a way to return a double array in c++
    the array is int theBord[8][8].

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    There is no way to return even a simple array in C++.

    Use std::vector< std::vector<int> > or create a struct that contains an array.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    13
    You can pass single dimensional arrays around with pointers like:
    Code:
    #include <iostream>
    
    void passing_single_array(int* array_you_passed);
    
    int main(void)
    {
    	int tmp_array[3];
    
    	passing_single_array(tmp_array);
    
    
    	std::cout << tmp_array[0] << " " << tmp_array[1] << " " << tmp_array[2] << std::endl;
    
    	return EXIT_SUCCESS;
    }
    
    void passing_single_array(int* array_you_passed)
    {	
    	array_you_passed[0] = 0;
    	array_you_passed[1] = 1;
    	array_you_passed[2] = 2;
    }
    Unsure about two dimensional arrays, although char **argv works like this, doesn't it?? *Anyone*

    But like Sang-drax said - a struct containing an array is good

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > or create a struct that contains an array.
    You could be setting yourself up for a lot of hidden copying of large amounts of data if you do this.

    > Is there a way to return a double array in c++
    Why are you doing this?
    Arrays are passed as pointers, so there is no real need to also return that as a pointer.
    Code:
    void foo ( int array[8][8] ) {
      // do stuff
      // this will change the array in the caller
    }
    
    int main ( ) {
      int board[8][8];
      foo( board );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    13
    Wow,

    Thanks alot Salem, that'll save alot of time and effort in the future!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  4. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM