Advertisement

passing a two dimensional array

Started by April 19, 2001 09:15 AM
6 comments, last by soehrimnir 23 years, 9 months ago
Hello, I wanted to ask how I could pass a two dimensional array to a function. I tried the following, but it doesn't seem to work quite well.
  
void BlaBla(int *startpos, int width, int height) {
  for (int i=0; i<width; i++) {
    for (int j=0; j<height; j++) {
      if (*startpos == ...) {
        ...
      } else {
        ...
      }
      startpos++;
    }
  }
}

void Bla() {
  int grid[20][20];
  BlaBla(&grid[0][0], 20, 20);
}
  
Is this good? Edited by - soehrimnir on April 20, 2001 3:57:24 AM
In the spirit of your code, you could do something like this:

  void BlaBla (int * array, int width, int height){   for (int i=0; i < height; ++i)   {      for (int j=0; j < width; ++j)      {         // code to access array elements here         // use array[j] for each element<br></font><br>      }<br>   }<br>}<br><font color="blue">void</font> Bla ()<br>{<br>   <font color="blue">int</font> grid[<font color="purple">20</font>][<font color="purple">20</font>];<br>   BlaBla(grid, 20, 20);<br>}<br>  </pre></font></td></tr></table></center><!–ENDSCRIPT–><br><br>Basically, you can use nested loops to access each element, and you don''t have to pass "&grid[0][0]" as your value, just the value "grid", since the array variable is, by definition, a pointer to that memory.<br><br>Hope that helps.<br><br>-Hyren<br>    <br><br>"Back to the code mines… ka-chink… ka-chink…"    
"Back to the code mines... ka-chink... ka-chink..."Tachyon Digital - Down for the summer, be back in the fall.
Advertisement
Here''s something that I was tought by a teacher.

  typedef int array[20][20] // Yes, I typedef''d the array.int BlahBlah( array startpos, ... ){  // do what ever}void blah( void ){   array something;  // yep, this is an array of [20][20]      BlahBlah( something, ... );}  


this is the easiest way to do what you were talking about and you don''t have to worry about how to pass the array. The system knows...



"And that''s the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!
[Cyberdrek | ]
Cyberdrek- tell your teacher he sux!
what if you have 10 arrays each has different size??
so yeah right, you''ll typedef 10 types, NAH!

so the first way is fine!
your way is good just when you know that you''re going to use a specific array-size in all functions.

http://qsoft.cjb.net
Arkon
how about keeping it all dynamic?



#include

void foo (int ** array, int width, int height)
{ for (int i=0; i <= height; ++i)
{
for (int j=0; j <= width; ++j)
{
array[j] = i+j;
cout<[j]<<"\t";<br> }//end for(i)<br> <br> cout<<endl;<br> } //end for(i)<br>}//end foo()<br><br><br>void main ()<br>{<br> int grid[20][20];<br> BlaBla(grid, 20, 20);}<br><br> return EXIT_SUCCESS;<br><br>}//end main()<br></cpp><br><br>(i hope the less-than''s and the greater-than''s come out right.. <br>grr..why can''t i preview? </i>
Hello again,

I tried the answers of you all, but none of them worked. So I managed to set up the following which worked:

  #include <iostream.h> void BlaBla(int *startpos, int width, int height) {  for (int i=0; i<height; i++) {    for (int j=0; j<width; j++) {      cout << *startpos << "\t";      startpos++;    }    cout << endl;  }}int main() {  int grid[20][20];  // filling with some test data  int k=0;  for (int i=0; i<20; i++) {    for (int j=0; j<20; j++) {      grid[i][j] = k++;    }  }  BlaBla((int *)grid, 20, 20);  // had to do the cast here  return 0;}  


Thanks anyway

Advertisement
when you say that works, do you mean ''it works'' or do you mean ''it compiles, but does something weird when you run it''?

a 2d array is a pointer to an array of pointers, each of which in turn point to arrays. So in your function header you need:

  void BlaBla(int **startpos, int width, int height) {  ...}  


startpos++ will step you through one dimension, *startpos++ (theoretically) steps you through the other, but I wouldnt do that because if the compiler lets you, it will screw up your array.
When I write ''it works'' I definitely mean that it is compiling and that the program is doing what I wanted it to do. So in this case I will get an output of 400 numbers, ranging from 0 to 400.

And if you say that *startpos++ is theoratically correct, but I shouldn''t use it, then how do I use it? Is it something like this:

  void BlaBla(int **startpos, int width, int height) {  for (int i=0; i<height; i++) {    for (int j=0; j<width; j++) {      cout << startpos[i][j] << "\t";    }    cout << endl;  }  }...BlaBla((int **)grid, 20, 20);...  


When I try to do that, I get a segmentation fault (in Linux) which of course causes my program to crash.

This topic is closed to new replies.

Advertisement