assigning 2d array to type **arr doesn't work

This is a discussion on assigning 2d array to type **arr doesn't work within the C++ Programming forums, part of the General Programming Boards category; I have a program that uses 2 2d arrays, grid1 and grid2. I have a 'bool which' that tells me ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    assigning 2d array to type **arr doesn't work

    I have a program that uses 2 2d arrays, grid1 and grid2. I have a 'bool which' that tells me which grid to use. I would like to be able to use 1 grid name throughout the program (as opposed to grid1 and grid2).. Here's what I tried, but it doesn't appear to be working. Any insight?

    Code:
    enum cell {DEAD, ALIVE};
    
    const int WIDTH = 100;
    const int HEIGHT = 100;
    
    cell grid1[WIDTH][HEIGHT];
    cell grid2[WIDTH][HEIGHT];
    
    int main {
       bool which = true;
       ...
       cell grid[WIDTH][HEIGHT];
       if (which) {
          grid = grid1;
       }
       else {
          grid = grid2;
       }
       ...
    }
    It compiles fine, but the assignment seems to never take place. I also tried cell **grid = grid1, but that doesn't work either. Any help?

    Thanks!

    -Sean

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Either copy the array element by element, or declare "grid" as a pointer to a pointer. Note, if you do the second option, changing "grid" will actually be changin "grid1" or "grid2"
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    So that would be...

    cell **grid = grid1;

    Or something similar to that?

    I tried the above with gcc 2.95.2 (required for this assignment, can't use 3.4 or whatever it is) and received the following error...

    life.cpp:197: initialization to `cell **' from `cell (*)[100]'


    Copying the array element by element isn't an option since the 2d array may become larger and time is somewhat critical to this project (updating the screen).

    Thanks again

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    when you assign gridx to grid - do you want a copy of gridx to be created - or do you just want a pointer to gridx?

    Code:
    enum cell {DEAD, ALIVE};
    
    const int WIDTH = 100;
    const int HEIGHT = 100;
    
    cell grid1[WIDTH][HEIGHT];
    cell grid2[WIDTH][HEIGHT];
    
    int main {
       bool which = true;
       ...
       cell (*p_grid)[WIDTH][HEIGHT];
       if (which) {
          p_grid = &grid1;
       }
       else {
          p_grid = &grid2;
       }
       ...
    }
    note that in this case no copy would be created - thus when you use p_grid it will change the original grid.
    instead of pointer you could also use references

    [edit]
    also note that [] has higher precedence than *.
    thus the parenthesis in (*p_grid)[][] is neccessairy
    signature under construction

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Thanks, that works perfectly. I appreciate the help!

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I know this problem is already solved, but I would just like to point out that a static 2d array is still a pointer, not a pointer to a pointer.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Or even
    Code:
       cell (*p_grid)[HEIGHT];
       if (which) {
          p_grid = grid1;
       }
       else {
          p_grid = grid2;
       }
    Then you can just use p_grid[row][col] right off the bat without any further pointer notation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Quote Originally Posted by Salem
    Or even
    Code:
       cell (*p_grid)[HEIGHT];
       if (which) {
          p_grid = grid1;
       }
       else {
          p_grid = grid2;
       }
    Then you can just use p_grid[row][col] right off the bat without any further pointer notation.
    That's even better, thanks salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 11:34 AM
  3. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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