function returning a pointer :(

Started by
7 comments, last by Zahlman 14 years, 2 months ago
i am trying to have a function to create my array for the maze and then return it to the calling function... here is my code and my errors :) -thx //function int* createMaze(ifstream& mazeF,int& column,int& row) { int *mazeArray = new int[column][row]; return mazeArray; } //statement to catch return from function above mazeArray = createMaze(mazeFile,column,row); //errors 1>c:\documents and settings\m1k3\desktop\programming\c++\class\assignment_3\maze\maze\maze.cpp(50) : error C2540: non-constant expression as array bound 1>c:\documents and settings\m1k3\desktop\programming\c++\class\assignment_3\maze\maze\maze.cpp(50) : error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Advertisement
Your problem is not in the return, but in the way you create the array. In C++ arrays are one dimensional, but you can create arrays of arrays, which is what you're trying to do. But you only new one of the dimensions and the other is a static array where the size must be known at compile time.

You can fix it in two ways. Either change your code to something like
int ** mazeArray = new int *[column];for( int i = 0; i < column; ++i ) {   mazeArray = new int[row];}

In the second method you need to realize that you can flatten a 2D array into a 1D array by changing the way you index into it. Like this
int * mazeArray = new int[column*row];// Coordinate to indexint x = 6;int y = 3;int mazeCell = mazeArray[x + y * column];// Index to coordinateint index = 42int x = index % column;int y = index / column;

It's complaining about this line:

int *mazeArray = new int[column][row];

Try:
int *mazeArray = new int[column*row];

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I would recommend against using raw pointers. Use std::vector<> or boost::multi_array<> instead.
edited***

nvm buckeye is wrong lol ill study the above examples

not totally understanding promethiums code :/
Quote:Original post by nuclear123
nvm buckeye is wrong lol ill study the above examples

Um no, Buckeye is right. And so is everyone else in this thread for that matter.
sorry buckeye you are correct
Quote:Original post by nuclear123
not totally understanding promethiums code :/

Alright, perhaps some annotations will help:

int * mazeArray = new int[column*row];

This 'picture' shows you what the two dimensional array would look like, as well as its one dimensional equivalent. Notice how they have the same number of cells.
I'm using column = 3, row = 3 here.
   x →y  |---|---|---|↓  | 0 | 1 | 2 |   |---|---|---|   | 3 | 4 | 5 |   |---|---|---|   | 6 | 7 | 8 |   |---|---|---|   i →   |---|---|---|---|---|---|---|---|---|   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |   |---|---|---|---|---|---|---|---|---|

// Coordinate to indexint x = 6;int y = 3;int mazeCell = mazeArray[x + y * column];

Let's say we want to find the index of element x = 1, y = 2 (second column, third row).
From the above picture, you can see that's index is 7.

To find the index, we can use the following formula: (y * number_of_columns) + x
Using x = 1 and y = 2: (2 * 3) + 1 = 7

// Index to coordinateint index = 42int x = index % column;int y = index / column;

To extract the x and y coordinates from an index, we can use the following:

x = index % number_of_columns
y = floor(index / number_of_columns)

Again, using index = 7:

x = 7 % 3 = 1
y = floor(7 / 3) = 2

Note that since we're converting to integers, we don't need to actually floor the result:
7 / 3 = 2.333, but (int)2.333 = 2
Quote:Original post by rip-off
I would recommend against using raw pointers. Use std::vector<> or boost::multi_array<> instead.


Quoted for emphasis.

You want boost::multi_array for a grid that must be rectangular, and a vector of vectors for data where each "row" should have its own length.

This topic is closed to new replies.

Advertisement