Thread: 2D arrays

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    40

    2D arrays

    So i can't seem to figure this one out. How do i pass a 2D array to a function by reference, or as a pointer, or any way that will allow me to edit it and have it change the original? I guess a better description would be. I have a multiple classes that need to access and edit the contents of a 2d array. so how can i have each class store a pointer or a reference to the main array so that they can read and edit it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Adamkromm
    How do i pass a 2D array to a function by reference, or as a pointer, or any way that will allow me to edit it and have it change the original?
    If you are trying to change the array itself, then that is not possible as you cannot assign to an array. If you are trying to change the contents of the array, then the same idea applies: when passed as an argument, an array is converted to a pointer to its first element. It so happens that in this case the first element is itself an array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    yea, i meant to say, to change the contents of the array. Could you show an example of how this is done?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Well... don't you know how to change the contents of a 1D array using a pointer to its first element? It is not that much different, except that you would be changing an element of an element of the 2D array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    if i understand you correctly you mean something like this?:
    Code:
    void foo(int **t)
    {
        int **temp;
        temp = t;
        *temp[1] = 1;
    }
    
    void main()
    {
        int an_array[3][2] = {{0,0,0},{0,0,0}};
        foo(an_array);
        return 0;
    }
    when i try this it says
    Code:
    error: cannot convert ‘int*’ to ‘int**’ for argument ‘1’ to ‘void test(int**)’
    i can get it to work with a 1d array...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Actually, I mean something like this:
    Code:
    void foo(int t[][3])
    {
        t[0][1] = 1;
    }
    
    int main()
    {
        int an_array[2][3] = {{0,0,0},{0,0,0}};
        foo(an_array);
        return 0;
    }
    Notice that the parameter is a pointer to an array of 3 ints, not a pointer to a pointer. I changed an_array to be an array of 2 arrays of 3 ints, since that corresponds to the initialiser that you used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    ok, but lets say that foo is a class instead of a function. How can i get it to keep, a reference to the original, so that at a later time foo can change the original without needing to pass it as a reference again?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Adamkromm
    ok, but lets say that foo is a class instead of a function. How can i get it to keep, a reference to the original, so that at a later time foo can change the original without needing to pass it as a reference again?
    You can do the same thing: store a pointer to the first element of the 2D array. The syntax would be the more complicated looking:
    Code:
    class Foo
    {
    public:
        // ...
    private:
        int (*t)[3];
    };
    but in the end the t member variable is just a pointer to an array of 3 ints.

    A warning though: you have to be careful that the array still exists. It is possible that the 2D array is destroyed before the Foo object is destroyed, in which case the t member variable would point to an array that no longer exists.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    40
    it seems the error i was makeing is that i was trying to do int *array[3] instead of int (*array)[3] whats the difference?

  10. #10
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Adamkromm View Post
    it seems the error i was makeing is that i was trying to do int *array[3] instead of int (*array)[3] whats the difference?
    int *array[3] = pointer to an array of 3 ints.
    int (*array)[3] = an array of 3 pointers to ints
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Eh, IceDane mixed them up. It should be:
    int *array[3] = an array of 3 pointers to ints
    int (*array)[3] = pointer to an array of 3 ints.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by laserlight View Post
    Eh, IceDane mixed them up. It should be:
    int *array[3] = an array of 3 pointers to ints
    int (*array)[3] = pointer to an array of 3 ints.
    Oh, ..........

    Really? That is almost counter-intuitive, if you ask me. The parenthesis in the latter are begging to be read first, thus "pointer to int" and then "[3]" = array of 3 = array of 3 pointers to int.

    Ah well.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by IceDane View Post
    Oh, ..........

    Really? That is almost counter-intuitive, if you ask me. The parenthesis in the latter are begging to be read first, thus "pointer to int" and then "[3]" = array of 3 = array of 3 pointers to int.

    Ah well.
    The joys of the syntax .... arrays of pointers and pointers to arrays are concepts that require some effort to understand ..... any syntax that could be chosen will be obvious to someone but will be counter-intuitive to someone else.

    The logic in "int (*array)[3];" is (roughly) that (*array) is an array of three ints. So array is a pointer to an array of three ints.

    Whereas, in "int *array[3];" array is .... an array of three elements that point at an int.

    Whether that's intuitive or not is a topic for discussion over a beer .... preferably after many beers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by IceDane View Post
    Oh, ..........

    Really? That is almost counter-intuitive, if you ask me. The parenthesis in the latter are begging to be read first, thus "pointer to int" and then "[3]" = array of 3 = array of 3 pointers to int.

    Ah well.
    It shouldn't be.
    Let's look at some examples.

    int n[3];

    Well all know what this is. It's an array of 3 ints, right? What's left of the name, n, is the type of the array, yes?
    Let's add a * to that:

    int* n[3];

    Now we see that it's still an array. But this time it's an array of int*, right?
    If we then consider a special syntax, of a pointer to an array to be:

    int (*)[3];

    ...And then we add the name inside there, and we get

    int (*n)[3];

    Makes more sense then. Right?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Elysia View Post
    It shouldn't be.
    Let's look at some examples.

    int n[3];

    Well all know what this is. It's an array of 3 ints, right? What's left of the name, n, is the type of the array, yes?
    Let's add a * to that:

    int* n[3];

    Now we see that it's still an array. But this time it's an array of int*, right?
    If we then consider a special syntax, of a pointer to an array to be:

    int (*)[3];

    ...And then we add the name inside there, and we get

    int (*n)[3];

    Makes more sense then. Right?
    Ah, yes. It does with the * at the type, not at the variable. I see why C++ people want to do that.

    Thanks. That was pretty embarrassing. I should know this stuff.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  2. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 07:33 AM