Thread: Return array

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    Post Return array

    Is it possible that we can return a multi-dimension array in C as in Java?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by Dave_Sinkula
    No.

    Perhaps a snippet of your attempt to do something...?
    False.

    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;
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    a.
    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;
    }
    I don't know if the malloc() usage is correct as I haven't played with C much.

    b. It's a pointer to a pointer to a templated type; therefore, a two-dimension array.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    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.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

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