Advertisement

2D Arrays

Started by July 18, 2001 08:03 PM
0 comments, last by b0b 23 years, 7 months ago
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.

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 arrayvoid 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.





This topic is closed to new replies.

Advertisement