Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

b0b

2D Arrays

This topic is 6305 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

How do you pass 2d arrays to a function and how do you modify it within the function? Im working on a random map generator for my game and i can''t figure out how to use multidimensional arrays in functions. PLEASE don''t reply unless youre sure it works like you tried your idea out with VC++ 6.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

Aren''t we demanding?

This is a very basic C/C++ concept, so you might want to brush up. Anyway, here is a method to pass an array:

  

#define CX 100
#define CY 100

// this is the function that takes an array


void fillarray(int* array, int val)
{
for(int w=0; w<(CY*CX); w++)
array[w] = val;
}

int myarray[CY][CX]; // declare an array of ints


fillarray(myarray, 100); // fill array with 100




You should know that using array without brackets after the array name acts as a pointer to the array. So in reality you pass a pointer to an array. Note also that when you pass an array this way, the function has no idea what dimensions it is (why I used #defines)

There are other ways to do it. This is the most basic.





Share this post


Link to post
Share on other sites

  • 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!