Thread: Returning a pointer to a 2d array?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Returning a pointer to a 2d array?

    I'm trying to return a pointer to a 2D static array. What I've tried is

    Code:
    class Foo
    {
    public:
        void * get_array_ptr();
    
    private:
        typedef int arr_t[10];
        arr_t foo_array[10];
    }
    
    void * Foo::get_array_ptr()
    {
        return foo_array;
    }
    I want to use the value get_array_ptr returns just like I'd use a 2D static array, for example

    Code:
    class Bar : Foo
    {
         void process();
    }
    
    void Bar::process()
    {
        arr_t x = get_array_ptr(); //compile error here
        int* val = x[2];
        int val2 = val[3];
    }
    Of course, this doesn't compile. I get the error "array must be initialized with a brace enclosed initializer". Does anyone have any suggestions on how to pull this off?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You could dynamically allocate it:
    Code:
    class Foo {
    public:
        typedef int** arr_t;
        // you may want a couple of consts defining the dimensions
        arr_t get_array_ptr();
        Foo();  // create array
        ~Foo(); // destroy array
    private:
        arr_t foo_array;
    };
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Why do you want to use void* there?

    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef int arr_t[10];
    
    class Test {
    		arr_t ray[3];
    	public:
    		Test () {}
    		arr_t *getray () { return ray; }
    };
    
    int main(void) {
    	Test *eg = new Test();
    	arr_t *x = eg->getray();
    	int n = x[2][3];
    	cout << n << endl;
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Quote Originally Posted by MK27 View Post
    Why do you want to use void* there?
    It's not the full picture. Foo actually extends another class that had get_array as a virtual function. Its return type really isn't defined. But I found a way to accomplish my goal using
    Code:
    typedef struct { int arr[10]; } my_struct;
    and then making an array of that struct as a member var. The only hang up is I need to reference the struct's member before I do the 2nd index, which shouldn't be too big of an issue.

    P.S. I can't even put a 1 liner in without code tags? Getting strict now, huh?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a pointer to a struct array
    By osiris^ in forum C Programming
    Replies: 14
    Last Post: 09-13-2009, 10:21 AM
  2. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  3. Returning a pointer
    By Canadian0469 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2008, 04:06 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM