Jump to content
  • Advertisement
Sign in to follow this  
nuclear123

function returning a pointer :(

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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 index
int x = 6;
int y = 3;
int mazeCell = mazeArray[x + y * column];

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


Share this post


Link to post
Share on other sites
It's complaining about this line:

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

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 index
int 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 coordinate
int index = 42
int 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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!