Passing 2D arrays to function

I am trying to make an ASCII world, however I am unable to pass 2D arrays between functions. It is a 20 x 20 array, and I want to place houses randomly across it. The array won't pass like I want it to, and my tutorial told me that global variables are evil, so a solution without those would be great.

using namespace std;

void place_house(const int width, const int height, string world[width][length])
{
    int max_house    = (width * height) / 10; //One tenth of the map is filled with houses
    int xcoords = (0 + (rand() % 20));
    int ycoords = (0 + (rand() % 20));
    world[xcoords][ycoords] = "@";
}

int main(int argc, const char * argv[])
{
    srand((unsigned)time(NULL));
    const int width  = 20;
    const int height = 20;
    string world[width][height];
    string grass    = ".";
    string house    = "@";
    string mountain = "^";
    string person   = "Å";
    string treasure = "$";
    //Fill entire world with grass
    for (int iii = 0; iii < 20; ++iii) {
        for (int jjj = 0; jjj < 20; ++jjj) {
            world[iii][jjj] = ".";
        }
    }
    place_house(width, height, world);
    for (int iii = 0; iii < 20; ++iii) {
    for (int jjj = 0; jjj < 20; ++jjj) {
        cout << world[iii][jjj] << " ";
        }
        cout << endl;
    }
}

Answers


Try passing string ** instead of string[][]

So your function should be declared like this:

void place_house(const int width, const int height, string **world)

and then you access your array usual way.

Remember to handle bounds correctly (probably you want to pass them together with array).


edit:

This is how you might achieve what you need:

#include <string>
#include <iostream>
using namespace std;

void foo (string **bar)
{
    cout << bar[0][0];
}

int main(void)
{
    string **a = new string*[5];
    for ( int i = 0 ; i < 5 ; i ++ )
        a[i] = new string[5];

    a[0][0] = "test";

    foo(a);

    for ( int i = 0 ; i < 5 ; i ++ )
        delete [] a[i];
    delete [] a;
    return 0;
}

edit

Another way of achieving what you want achieve (that is passing static array to a function) is to pass it as one dimmensional array and then use C-like way of accessing it.

Example:

#include <string>
#include <iostream>
using namespace std;

void foo (string *bar)
{
    for (int r = 0; r < 5; r++)
    {
        for (int c = 0; c < 5; c++)
        {
            cout << bar[ (r * 5) + c ] << " ";
        }
        cout << "\n";
    }
}

int main(void)
{
    string a[5][5];
    a[1][1] = "test";
    foo((string*)(a));
    return 0;
}

This little example is nicely described here (see Duoas post).

So I hope this will describe different ways of doing similar thing. This, however, does look quite ugly and probably is not the best programming practice (I would do everything to avoid doing that this way, dynamic arrays are quite nice you just need to remember releasing them).


Need Your Help

How to add a word before your domain name on a website?

python django url dns

I don't know if this is relevant, but I am using Django to build a website.

A little confused about the modifiers of Java

java oop access-modifiers

I'm learning Java language. A little confused about the modifier.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.