Thread: pointer to matrix

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    pointer to matrix

    I have two different twodimensional matrixes of the same size

    Code:
    Square Matrix1[MTR_WIDTH][MTR_HEIGHT];
    Square Matrix2[MTR_WIDTH][MTR_HEIGHT];
    Now I am only using one of them at a time, the other serves as a storage matrix in the mean time. So I wan't to have a pointer pointing to the one currently in use. I have tried this:

    Code:
    Square *CurrentMatrix[MTR_WIDTH][MTR_HEIGHT] = Matrix1;
    But it doesn't work, "conflicting declaration 'Square*CurrentMatrix[160][120]' ".

    I have tried some different ways but can't figure out how to write it. Can someone help me please?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Something like this is possible.

    Code:
    #include <iostream>
    
    const int W = 2;
    const int H = 2;
    
    void print(int* array)
    {
        for (int i = 0; i < W; i++) {
            for (int j = 0; j < H; j++) {
                std::cout << *array++ << " ";
            }
        std::cout << std::endl;
        }
    }
    
    int main()
    {
        int a[W][H] = {{33, 34}, {35, 36}};
        int b[W][H] = {{100, 110}, {120, 130}};
        int* c;
        c = &a[0][0];
        print(c);
        c = &b[0][0];
        print(c);
        std::cin.get();
    }
    In your case you would need a Square pointer initialized to the first element in the 2D array. It is your responsibility to know, how many Squares you may access with this pointer (MTR_WIDTH*MTR_HEIGHT) using pointer arithmetics.

    (By the way, you can pass 2D arrays to functions as well if you changed the signature. Then you wouldn't need c at all.)
    Last edited by anon; 01-02-2007 at 06:22 PM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You can also use pointer to array
    Code:
    #include <iostream>
    
    const int W = 2;
    
    void print(int(* array)[W],int height)
    {
        for (int i = 0; i < W; i++) 
        {
            for (int j = 0; j < height; j++) 
            {
                std::cout << array[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }
    
    int main()
    {
        const int H = 2;
        int a[W][H] = {{33, 34}, {35, 36}};
        int b[W][H] = {{100, 110}, {120, 130}};
        int(* c)[W];
        c = a;
        print(c,H);
        c = b;
        print(c,H);
        std::cin.get();
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Maybe only Square** pointer_to_current_2d_array; will do the trick
    you could evaluate it like this: pointer_to_current_2d_array = &Matrix1;

    Imho it should work.
    Programming is a form of art.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by hardi
    Maybe only Square** pointer_to_current_2d_array; will do the trick
    you could evaluate it like this: pointer_to_current_2d_array = &Matrix1;

    Imho it should work.
    I think you should try to compile it before suggesting
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131

    didn't work

    Quote Originally Posted by vart
    I think you should try to compile it before suggesting
    yep, It didn't work. sorry
    Programming is a form of art.

  7. #7
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Thanks vart! It worked perfectly fine. I solved it like this:

    Code:
    Square Matrix1[MTR_WIDTH][MTR_HEIGHT];
    Square Matrix2[MTR_WIDTH][MTR_HEIGHT];
    Square (*CurrentMatrix)[MTR_HEIGHT] = Matrix1;
    Also anon's way worked, but it was a little bit more complicated in that case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  4. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  5. Help!! Tips on Matrix Calculator Program please!
    By skanxalot in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2002, 11:26 AM