# function returning a pointer :(

This topic is 3069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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;

##### Share on other sites

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

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

##### Share on other sites
I would recommend against using raw pointers. Use std::vector<> or boost::multi_array<> instead.

##### Share on other sites
edited***

nvm buckeye is wrong lol ill study the above examples

not totally understanding promethiums code :/

##### Share on other sites
Quote:
 Original post by nuclear123nvm 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.

##### Share on other sites
sorry buckeye you are correct

##### Share on other sites
Quote:
 Original post by nuclear123not 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

##### Share on other sites
Quote:
 Original post by rip-offI 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.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631740
• Total Posts
3001966
×