Thread: 2D Array of Pointers

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    11

    2D Array of Pointers

    I'm trying to create a two-dimensional array of pointers but I don't know how to initialize it.

    What I'm trying to do overall is create a text-based adventure game (like Zork). The pointers point to either strings or char arrays with the descriptions of the rooms. I was thinking of using char arrays because the names of them can be treated as pointers. So say I have the following:
    Code:
    char[] a01 = "Description 1";
    char[] a02 = "Description 2";
    char[] a03 = "Description 3";
    
    *** map[2][2]{
       {*a01, *a02}
       {*a03}
    }
    What would be the initializer that I'd put where the asterisks are? And do I have the pointers formatted correctly (never used them before)? And should I use strings or other over char arrays?

    Thanks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's one way, but I don't know if this is what you really want to do.
    Code:
    #include <iostream>
    
    int main(void)
    {
      char  a01[] = "Description 1";
      char  a02[] = "Description 2";
      char  a03[] = "Description 3";
      char  a04[] = "Description 4";
      
      char *map[2][2] = {{a01, a02}, {a03, a04}};
      
      std::cout << map[0][0] <<std::endl
                << map[0][1] <<std::endl
                << map[1][0] <<std::endl
                << map[1][1] <<std::endl;
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    5
    I'd recommend using an std::map instead of the 2d array, and just giving each room a unique "id" (and use this id as the key for the map).

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    11
    Thanks. I'll try using the map thingy (command/function). Who would've thought -- using 'map' for a map?

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    11
    Okay, I tried using the map variable/whatever. It doesn't compile.
    Code:
    map<string, string> rooms;
    rooms["a00"] = "This is the room 0,0\n";
    rooms["a01"] = "This is the room 0,1\n";
    rooms["a03"] = "This is the room 0,3\n";
    size of array `rooms' has non-integer type
    conflicting types for `int rooms[1]'
    previous declaration as `std::map<std::string, std::string, std::less<std::string>, std::allocator<std:air<const std::string,

    I don't know, it doesn't seem to be recognizing the map the way it's supposed to.

    I'm using Dev-C++ 4.9.8.7
    Thanks

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't know, it doesn't seem to be recognizing the map the way it's supposed to.
    Post more code.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2004
    Posts
    11
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <map>
    #include <string>
    
    using namespace std;
    
    // Globals //
    
    int x; //x coordinate for map
    int y; //y coordinate
    string input;
    string wall = "Wall.";
    map<string, string> rooms;
    rooms["a00"] = "This is the room 0,0\n";
    rooms["a01"] = "This is the room 0,1\n";
    rooms["a03"] = "This is the room 0,3\n";
    rooms["a10"] = "This is the room 1,0\n";
    rooms["a12"] = "This is the room 1,2\n";
    rooms["a13"] = "This is the room 1,3\n";
    rooms["a14"] = "This is the room 1,4\n";
    rooms["a20"] = "This is the room 2,0\n";
    rooms["a21"] = "This is the room 2,1\n";
    rooms["a24"] = "This is the room 2,4\n";
    rooms["a30"] = "This is the room 3,0\n";
    rooms["a34"] = "This is the room 3,4\n";
    rooms["a40"] = "This is the room 4,0\n";
    rooms["a41"] = "This is the room 4,1\n";
    rooms["a42"] = "This is the room 4,2\n";
    rooms["a43"] = "This is the room 4,3\n";
    rooms["a44"] = "This is the room 4,4\n";
    string maparea[4][4]{
       {"a00","a01",'\n',"a03",'\n'}
       {"a10",'\n',"a12","a13","a14"}
       {"a20","a21",'\n','\n',"a24"}
       {"a30",'\n','\n','\n',"a34"}
       {"a40","a41","a42","a43","a44"}
    };
    This is everything that would factor into the map. A function will call on the array later, but whatever. And I don't know if that's the right way to declare that array.

    I kept the array, because it was the easiest way that I could think of for moving around, 'cause I'm currently going to use sort of x,y coordinates. So moving west would cause x to decrease by 1.

    Any thoughts...?

    Edit: By the way, the first code error is on the first 'rooms' saying that I can't declare it without a type.
    Last edited by Slavakion; 03-30-2004 at 02:18 PM. Reason: Add something

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Is all of that actually in the global scope? Or did you just remove the enclosing functions? If it's the former then you need to do this (the two dimensional array has been fixed too, see below):
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <map>
    #include <string>
    
    using namespace std;
    
    // Globals //
    
    int x; //x coordinate for map
    int y; //y coordinate
    string input;
    string wall = "Wall.";
    map<string, string> rooms;
    string maparea[5][5] = {
      {"a00","a01","\n","a03","\n"},
      {"a10","\n","a12","a13","a14"},
      {"a20","a21","\n","\n","a24"},
      {"a30","\n","\n","\n","a34"},
      {"a40","a41","a42","a43","a44"}
    };
    
    int main()
    {
      rooms["a00"] = "This is the room 0,0\n";
      rooms["a01"] = "This is the room 0,1\n";
      rooms["a03"] = "This is the room 0,3\n";
      rooms["a10"] = "This is the room 1,0\n";
      rooms["a12"] = "This is the room 1,2\n";
      rooms["a13"] = "This is the room 1,3\n";
      rooms["a14"] = "This is the room 1,4\n";
      rooms["a20"] = "This is the room 2,0\n";
      rooms["a21"] = "This is the room 2,1\n";
      rooms["a24"] = "This is the room 2,4\n";
      rooms["a30"] = "This is the room 3,0\n";
      rooms["a34"] = "This is the room 3,4\n";
      rooms["a40"] = "This is the room 4,0\n";
      rooms["a41"] = "This is the room 4,1\n";
      rooms["a42"] = "This is the room 4,2\n";
      rooms["a43"] = "This is the room 4,3\n";
      rooms["a44"] = "This is the room 4,4\n";
    }
    Now for the array:
    Code:
    string maparea[4][4]{
       {"a00","a01",'\n',"a03",'\n'}
       {"a10",'\n',"a12","a13","a14"}
       {"a20","a21",'\n','\n',"a24"}
       {"a30",'\n','\n','\n',"a34"}
       {"a40","a41","a42","a43","a44"}
    };
    The first problem is that you need the assignment operator after the subscripts to tell the compiler this is an initialization. The second is that you say a 4x4 matrix but initialize as a 5x5 matrix, change the dimensions as well:
    Code:
    string maparea[5][5] = {
    Next, string literals and character literals are different. All occurances of '\n' should be "\n" of you will get errors. Lastly, you need commas to separate each dimension:
    Code:
       {"a00","a01",'\n',"a03",'\n'},
       {"a10",'\n',"a12","a13","a14"},
       {"a20","a21",'\n','\n',"a24"},
       {"a30",'\n','\n','\n',"a34"},
       {"a40","a41","a42","a43","a44"}
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Jan 2004
    Posts
    11
    Okay, I cleaned it up a lot (less globals!). Now, I need to know how to display a value of an object in the map by using the key. My book doesn't really get into maps at all...

    EDIT: BTW, Yeah, it was all global...

    Notice the rather clunky movement function that doesn't deserve the title of parser

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <map>
    #include <string>
    
    using namespace std;
    
    //Prototypes //
    void parser();
    void wmap();
    
    // Globals //
    
    int x; //x coordinate for map
    int y; //y coordinate
    string input;
    string wall = "Wall.";
    
    // Main //
    
    int main(int argc, char *argv[])
    {
      x = 1; //Set start position
      y = 0; 
      wmap();
      while(input != "q" ){
         cin >> input;
         parser();
      }
      system("PAUSE");	
      return 0;
    }
    
    void parser(){
       if (input == "n" || input == "north"){
          if (y < 4){
          y++;
          }
          else{
          cout << wall << endl;
          }
       }
       else if (input == "s" || input == "south"){
          if (y > 0){
          y--;
          }
          else{
          cout << wall << endl;
          }
       }
       else if (input == "w" || input == "west"){
          if (x > 0){
          x--;
          }
          else{
          cout << wall << endl;
          }
       }
       else if (input == "e" || input == "east" ){
          if (x < 4){
          x++;
          }
          else{
          cout << wall << endl;
          }
       }
       wmap();
    }
    void wmap(){
       map<string, string> rooms;
       rooms["a00"] = "This is the room 0,0\n";
       rooms["a01"] = "This is the room 0,1\n";
       rooms["a03"] = "This is the room 0,3\n";
       rooms["a10"] = "This is the room 1,0\n";
       rooms["a12"] = "This is the room 1,2\n";
       rooms["a13"] = "This is the room 1,3\n";
       rooms["a14"] = "This is the room 1,4\n";
       rooms["a20"] = "This is the room 2,0\n";
       rooms["a21"] = "This is the room 2,1\n";
       rooms["a24"] = "This is the room 2,4\n";
       rooms["a30"] = "This is the room 3,0\n";
       rooms["a34"] = "This is the room 3,4\n";
       rooms["a40"] = "This is the room 4,0\n";
       rooms["a41"] = "This is the room 4,1\n";
       rooms["a42"] = "This is the room 4,2\n";
       rooms["a43"] = "This is the room 4,3\n";
       rooms["a44"] = "This is the room 4,4\n";
       string maparea[5][5] = {
       {"a00","a01","\n","a03","\n"},
       {"a10","\n","a12","a13","a14"},
       {"a20","a21","\n","\n","a24"},
       {"a30","\n","\n","\n","a34"},
       {"a40","a41","a42","a43","a44"}
       };
       cout << maparea[x][y] << endl;    <-- I must have to do something different here
    }

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    cout << maparea[x][y] << endl; <-- I must have to do something different here

    x and y have to be given values (between 0 and 4), like this:
    Code:
    for(x = 0; x < 5; ++x)
    {
    for(y = 0; y < 5; ++y)
    cout << maparea[x][y];
    }
    So far the map you have created isn't being used. The above just uses the 2D array of strings. To use the map you could try this:

    cout << rooms["a40"] << endl;

    or maybe something like this:

    Code:
    map<string, string>::iterator start = rooms.begin();
    map<string, string>::iterator stop = rooms.end();
    for( ; start != stop; ++start)
    cout << "room " << start->first << " has the following information : " << start->second << endl;
    to get a feel for what you can do with maps.
    Last edited by elad; 03-31-2004 at 10:25 AM.

  11. #11
    Registered User
    Join Date
    Jan 2004
    Posts
    11
    I definitely can't just write

    cout << rooms["a40"]

    because it's dependant on a variable. I'm looking to base the movement on x y coordinates and tie that to the array. So when you move, the array is checked, which has links to descriptions of the rooms.

    Your last example just seems to run through all of the values in the map, so that won't really work. Thanks anyway, though.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Yup, that's all my example was intended to do. But building on those examples I can now try to hook the 2D array to the map using a string from the 2D array identified by x and y. In particular I would try something like:

    int x = 2;
    int y = 0;
    cout << maparea[x][y] << endl;
    cout << rooms[maparea[x][y]];

    and I'd expect output to be:

    a20
    This is room 2, 0

    though I haven't confirmed that this will work. If you don't like the nested bracket syntax you could try this:

    int x = 2;
    int y = 0;
    string temp;
    temp = maparea[x][y];
    cout << temp << endl;
    cout << rooms[temp];



    As an alternative to this approach I'd avoid using the map completely. This would entail using a 2D array of rooms rather than a map of rooms hooked to a 2D array of strings by way of a key that is a string.

    Either approach should work though.

  13. #13
    Registered User
    Join Date
    Jan 2004
    Posts
    11
    Yay! It works! Now comes the more creative part, the descriptions ("This is the room 2, 0" just doesn't cut it).

    And the nested brackets don't bother me if they don't bother the compiler. In a perverse way, it's completely logical...

    Well, thanks! And I'll be sure to keep you updated on whatever I can do (I know that you're just jumping for joy).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM