Is it possible that we can return a multi-dimension array in C as in Java?
Is it possible that we can return a multi-dimension array in C as in Java?
No.
Perhaps a snippet of your attempt to do something...?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
False.Originally Posted by Dave_Sinkula
Code:template<class T> T** 2D_Array_Alloc(int x, int y) { T** obj = new T*[x]; for(int i = 0; i < x; i++) obj[i] = new T[y]; return obj; }
a: C board
b: what is that return type?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
a.I don't know if the malloc() usage is correct as I haven't played with C much.Code:char** 2D_CharArray_Alloc(int x, int y) { char** obj = (char**)malloc(x); for(int i = 0; i < x; i++) tmp[i] = (char*)malloc(y); return obj; }
b. It's a pointer to a pointer to a templated type; therefore, a two-dimension array.
A pointer is not an array (for the umpteenth time).
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
The only difference is how they are stored in the computer's memory. I believe arrays are on the heap and pointers (dynamically allocated, that is) on the stack or the other way around. I always mix them up.
http://c-faq.com/aryptr/index.html
[edit]FAQ > Explanations of... > Casting malloc
Didja know that identifiers cannot begin with a number?
What else may be wrong with your attempt at dynamically allocating memory?
Last edited by Dave_Sinkula; 05-22-2006 at 09:44 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*