Skip to main content
gamedev.net
🔒 Locked

function returning pointer to 2d array

Started by subflood Dec 17, 2004 at 10:23 PM 3 replies 600+ views
Original Post
subflood
subflood
I have a 2d array of type int in one of my functions. I'm not sure how the function prototype should look so I can return the 2d array and the return value will be assigned to another 2d array or a pointer to pointer. Again I'm not sure if the return value of the function should be assigned to a pointer to pointer or a 2d array.
mike25025
mike25025
i do it like this

typedef int array[100][100];
void foo(array& a);

but theres probably a better way
nts
nts
Quote:
Original post by subflood
I have a 2d array of type int in one of my functions. I'm not sure how the function prototype should look so I can return the 2d array and the return value will be assigned to another 2d array or a pointer to pointer. Again I'm not sure if the return value of the function should be assigned to a pointer to pointer or a 2d array.


If you are using C/C++ and define the array locally within the function then returning a pointer to that array wont work, after the function completes the array is gone and your pointer will point to who knows what. Your best bet is probably to pass in a pointer to the array you would like to fill and then either use that function and fill it from your temp array or directly write to it.

HTH
Jingo
Jingo
Something like this will work:

class eg{    int array[10][10];public:    int (*example())[10][10]    {        return &array    }};


EDIT: Should also note that you should not assign that return value to a pointer to pointer to int, as that will not work as you expect.

int (*pmyarray)[10][10] = myeginstance.example(); //correct use

subflood
subflood
/\/\/\/\/\/\/\/\/\/\/\/\/\ Thanks this is exactly what I was looking for as I'm still having some trouble understanding pointers to 2d arrays.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.