returning 2D arrays

This is a discussion on returning 2D arrays within the C++ Programming forums, part of the General Programming Boards category; i have a 2D array of ints. its a private member of my class and i want to keep it ...

  1. #1
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    returning 2D arrays

    i have a 2D array of ints. its a private member of my class and i want to keep it that way

    Code:
    class Class
    {
    private:
    
        int array[10][10];
    
    //...
    
    };
    the name of a 2D array is nothing more than a pointer to the first element of an array, which is in turn a pointer to the first element in its array.

    array is a pointer to array[0] which is a pointer to array[0][0].

    so i make a function that returns a 2D pointer.

    Code:
    int ** Class::function() 
    {
        return array;
    }
    but i always end up with an error like this:

    " cannot convert int[10][10] to int ** "

    now, if a 2D array is just a double pointer, why wont this work? how can i get to this outside of the class without making it public?
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    well, i wasnt planning on giving it full access to the pointer, i was going to make it a const... what i was saying was only an example.

    but thanks, that worked...
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Thanks, Salem, for answering that question so well. I thought one would simply line up asterisks till the cows came home :P
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 05:06 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  4. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 06:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21