So here's my question as I have been looking over the internet for a while but can't seem to get a clear answer to make it work. I am working on creating a simple sokoban game in C++ using the Console.
First of all, I am pretty sure that it is not a good idea to pass an array to a function due to the memory intensity if it is a large array. All fine and good, that should not be a problem as I only have a maximum of up to about 32x41 and I'm not even going to get to the maximum. So should I pass the whole array or should I pass pointers?
Secondly, My array size is not always the same. I'm not sure if this is an important factor or not.
Thirdly, If I'm going to be passing pointers, how do I create/initialise them before I start working with them. My array was created in the following manner:
string line;
string arr[30];
int i = 0;
char mazeArr[30][40];
int k, count;
if (mazeStream.is_open())
{
while ( mazeStream.good() && !mazeStream.eof() )
{
getline (mazeStream,line);
cout << line << endl;
arr[i] = line;
i++;
}
mazeStream.close();
cout << endl;
}
else cout << "Unable to open file";
for ( count = 0; count < 12; count ++)
{
string::const_iterator iterator1 = arr[count].begin();
k = 0;
while (iterator1 != arr[count].end())
{
mazeArr[count][k] = *iterator1;
iterator1++;
k++;
}
}
now what I would like to do with this 2D array is:
So in the end I will end up with a second array of instances, where each instance is dependent on the symbol I have taken from the first array. while retaining the same 'coordinates'
Any help would be greatly appreciated,
Thanks
I would do something along the lines of this:
char **createMaze(width, height)
{
// Dynamically allocate memory creating a pointer array of pointers
char **maze = new char*[width];
// Loop to allocate memory for each pointer array
for(int i = 0; i < width; i++)
maze[i] = new char[height];
return maze;
}
int main()
{
width = 40;
height = 30;
char **maze = createMaze(40, 30);
// You can now access elements from maze just like a normal
// 2D array maze[23][12] - You can also pass this into
// a function as an arugment
return 0;
}
This code is untested.. because I wrote it in the browser XD.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.