How do i return an array?

This is a discussion on How do i return an array? within the C++ Programming forums, part of the General Programming Boards category; I have a function that creates a 2d bool array--so the contents are either 0 or 1. Is there any ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    How do i return an array?

    I have a function that creates a 2d bool array--so the contents are either 0 or 1. Is there any way to return the array or to pass it as a pointer?

  2. #2
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    62
    Passing 2D array in function:

    Code:
    bool Boo [ X ] [ Y ] = { 0, 0 };//Variable declaration
    
    Function ( bool ( * Boo ) [ Y ] ){}//Function declaration
    
    Function ( Boo );//Function call
    This parameter is reserved

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Return a vector instead of a pointer. Although vector<bool> is a special case where they really screwed up when creating the C++ standard (since it isn't actually a vector of bools), so according to Scott Meyers in "Effective STL" some alternatives to vector<bool> are a bitset or deque<bool>. My own idea that I wrote to him about is about using basic_string<bool>.
    Anyways, you've got a lot of choices besides raw arrays.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    I see, but the function in this case is being called by another function, so sending it wouldn't work.

    Is there a way to just return the array without vectors?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Oh I see, I can have an array with

    bool box [ X ] [ Y ] = Function ( Boo ); -- Is that what you meant?

    - Sorry, I am just starting out, is there a simple way of doing this?
    Last edited by Fredir; 12-01-2007 at 09:46 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is VERY unusual in C or C++ to "expect" an array to be returned from a function. The more common method of doing such things is to pass in an array to be filled by the function, e.g.

    Code:
    void func(int arr[][10], int size)
    {
       for(int i = 0; i < size; i++) 
       {
           for(j = 0; j < 10; j++) 
           {
               arr[i][j] = i * j;
           }
        }
    }
    
    int main() 
    {
       int array[100][10];
        
       func(array, 100);
       return 0;
    }
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21