Passing multidimensional arrays to functions
As the subject implies, I was wondering how I''d go about passing multi-dimensional arrays (2D, in this case) to functions. Any suggestions?
D:
July 03, 2001 10:45 AM
Wether use: std::vector>
or just try to pass a simple pointer. Don''t really know if that works....
e.g.
void f(char *test)
{...}
void main()
{
char a[123][123];
f(a);
}
or just try to pass a simple pointer. Don''t really know if that works....
e.g.
void f(char *test)
{...}
void main()
{
char a[123][123];
f(a);
}
I gave it a shot, and it cut down my error count from seven to one(although it seems that I have to use a pointer to a pointer for my two-dimensional array). The code looks something like this:
However, I'm getting the following error:
Edited by - psychoprog on July 3, 2001 1:08:50 PM
|
However, I'm getting the following error:
error C2664: 'printLoc' : cannot convert parameter 1 from 'int' to 'int ** '
Edited by - psychoprog on July 3, 2001 1:08:50 PM
D:
This seems to compile just fine.
Jesse
|
Jesse
Website - Third Party Ninjas | Twitter - twitter.com/Chounard
PsychoProg:
The two-dimensional array you specify, int maps[10][10], can''t be directly cast into a int **ptr. Try just that line (int **ptr = maps) and you''ll get a "can''t convert" error.
The reason is that maps is of type int[10][10], which is fundamentally different than int **. When you use a multidimensional variable, the compiler needs to know what dimensions are there. To see why, look at these following arrays:
int ar1[2][50];
int ar2[4][25];
int ar3[10][10];
int ar4[25][4];
Each of these arrays has 100 ints, and the compiler can magically find its way to each element given two subscripts. But they''re each a different type completely, and none of them are int **. ar1[1][1] is pointing to the 51st element in ar1, whereas ar2[1][1] is pointing to the 26th. How are you supposed to get that different information from an int**? You can''t, therefore no conversion is available.
Let''s look at what an int** really is. If I have a int **ptr, what is ptr[0]? It''s an int*, so that''s expected to be an address. In the arrays above, the only thing you have in the array are ints, not int*. If I say ptr[0][2], it means that ptr[0] points to an integer array, and I want the second element of that pointer. So you can do multi-dimensional arrays that way (usually by using malloc), but your first dimension is actually an array of pointers to the second dimension arrays.
So, this is why what you had won''t work. What will work? It turns out that C needs to know everything but the first dimension of a multi-dimensional array in order to do its work. Examples of variables and what C needs to be able to reconstruct it (i.e. in a function):
int ar[X] needs int*
int ar[X][Y] needs int(*)[Y]
int ar[X][Y][Z] needs int(*)[Y][Z]
This makes sense if you think about it: if you know everything but the first dimension of an array, you know exactly how much you need to jump in memory in order to get to each row. You are free to specify every dimension, as Jesse did in his example:
void foo (int ar[10][10]) can accept a int a[10][10] parameter.
But you can''t pass an a[5][10] or an a[15][10]. However, you CAN do this:
void foo (int (*ar)[10]), and it will accept an int a[N][10], where N is any number. Within foo, you just use two-dimensional subscrips like normal: ar[2][3], etc.
Here''s an example program you can use to convince yourself:
The array is initialized so that you can see it all in a memory window if you wish, and the element [X][Y] will have the value of 10*X+Y (i.e. it will be base-10 representation of that number).
Keep in mind that if you do this, you have no way to dynamically change the dimensions of your arrays. If you need to do that, you must use a different approach, such as really using an int ** that points to an array of array pointers.
The two-dimensional array you specify, int maps[10][10], can''t be directly cast into a int **ptr. Try just that line (int **ptr = maps) and you''ll get a "can''t convert" error.
The reason is that maps is of type int[10][10], which is fundamentally different than int **. When you use a multidimensional variable, the compiler needs to know what dimensions are there. To see why, look at these following arrays:
int ar1[2][50];
int ar2[4][25];
int ar3[10][10];
int ar4[25][4];
Each of these arrays has 100 ints, and the compiler can magically find its way to each element given two subscripts. But they''re each a different type completely, and none of them are int **. ar1[1][1] is pointing to the 51st element in ar1, whereas ar2[1][1] is pointing to the 26th. How are you supposed to get that different information from an int**? You can''t, therefore no conversion is available.
Let''s look at what an int** really is. If I have a int **ptr, what is ptr[0]? It''s an int*, so that''s expected to be an address. In the arrays above, the only thing you have in the array are ints, not int*. If I say ptr[0][2], it means that ptr[0] points to an integer array, and I want the second element of that pointer. So you can do multi-dimensional arrays that way (usually by using malloc), but your first dimension is actually an array of pointers to the second dimension arrays.
So, this is why what you had won''t work. What will work? It turns out that C needs to know everything but the first dimension of a multi-dimensional array in order to do its work. Examples of variables and what C needs to be able to reconstruct it (i.e. in a function):
int ar[X] needs int*
int ar[X][Y] needs int(*)[Y]
int ar[X][Y][Z] needs int(*)[Y][Z]
This makes sense if you think about it: if you know everything but the first dimension of an array, you know exactly how much you need to jump in memory in order to get to each row. You are free to specify every dimension, as Jesse did in his example:
void foo (int ar[10][10]) can accept a int a[10][10] parameter.
But you can''t pass an a[5][10] or an a[15][10]. However, you CAN do this:
void foo (int (*ar)[10]), and it will accept an int a[N][10], where N is any number. Within foo, you just use two-dimensional subscrips like normal: ar[2][3], etc.
Here''s an example program you can use to convince yourself:
|
The array is initialized so that you can see it all in a memory window if you wish, and the element [X][Y] will have the value of 10*X+Y (i.e. it will be base-10 representation of that number).
Keep in mind that if you do this, you have no way to dynamically change the dimensions of your arrays. If you need to do that, you must use a different approach, such as really using an int ** that points to an array of array pointers.
July 03, 2001 02:56 PM
quote:
If I say ptr[0][2], it means that ptr[0] points to an integer array, and I want the second element of that pointer.
Actually that is saying ptr[0] points to an integer array, and I want the third element of that pointer. Remember arrays start with 0 so [2] would be the third element. [0][1][2] <-3rd

-SirKnight
If you want one function to take in several dynamic multidimensional arrays, you can do the following:
|
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement