Archived

This topic is now archived and is closed to further replies.

edwinnie

returning a 2D array of pointers...

Recommended Posts

ok i understand how to return a 1d array of pointers from a function. but how to do it in 2d? eg return type for 1d array of ptrs: Crystal** return type for 2d? Crystal*** heelps!! Edwin

Share this post


Link to post
Share on other sites
unfortunately that doesnt seem to work

i got this error stating unable to convert from
Crystal*[10][20] to Crystal***

heelps!

Edwin

Share this post


Link to post
Share on other sites
Here''s the deal with 1D versus 2D arrays.

A 2D array isn''t really a 2D array. After all, there''s no such thing as 2D memory; it''s all organized in a line. (Okay, just shut up, all the EE majors in the house.) So what your compiler does, when it sees something like this:

int a[5][6];

is it ACTUALLY allocates a line of 30 integers. Since it knows that each row has five values, when you access array element, say, [2][3], it multiplies 2 by 5 and adds 3.

But when you''re just returning an array like that, there''s no way for the compiler to know how long a row''s going to be. That''s why you can''t convert.

What most people do in this situation is make a 1D array, and manually do the element calculations that the compiler normally does for you. This is the most painless.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Good reply on the not really 2D arrays.

You can take that a whole step further even, and say that arrays aern''t really arrays

int booger[5][6];

for example the above is a multidemensional array right?

however the identifier booger is also a pointer This is dangerous; but also a really handy tool sometimes

Share this post


Link to post
Share on other sites