|
|
View previous topic :: View next topic |
Author |
Message |
micman Guest
|
Problem passing 2D array |
Posted: Mon Apr 05, 2004 6:35 am |
|
|
Hi all,
I have a problem, I do passing 2D (bidimensional) array in function es:
void main(){
int8 c_menu[5][3];
...
....
..
c_menu[0][0] = 1;
c_menu[0][1] = 8;
...
...
imposta_label(c_menu, sizeof(c_menu),0);
...
...
}
int8 imposta_label(int8 *g_menu, int8 size, int8 pos){
int8 read;
read = read_data_LCD(g_menu[pos][0]) - 0x30;
}
The error of compilation is: " Too many subscripts"
Why?? and how to resolve it?
Thanks all! |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Mon Apr 05, 2004 6:38 am |
|
|
Try
int8 imposta_label(int8 **g_menu, int8 size, int8 pos)
For your function definition and see if it works (notice the double **). |
|
|
micman Guest
|
|
Posted: Mon Apr 05, 2004 8:59 am |
|
|
int8 imposta_label(int8 **g_menu, int8 size, int8 pos){
read = read_data_LCD(g_menu[pos][0]) - 0x30;
}
Error the compilation "Expecting a, or ) |
|
|
Ttelmah Guest
|
Re: Problem passing 2D array |
Posted: Mon Apr 05, 2004 2:49 pm |
|
|
micman wrote: | Hi all,
I have a problem, I do passing 2D (bidimensional) array in finction es:
void main(){
int8 c_menu[5][3];
...
....
..
c_menu[0][0] = 1;
c_menu[0][1] = 8;
...
...
imposta_label(c_menu, sizeof(c_menu),0);
...
...
}
int8 imposta_label(int8 *g_menu, int8 size, int8 pos){
int8 read;
read = read_data_LCD(g_menu[pos][0]) - 0x30;
}
The error of compilation is: " Too many subscripts"
Why?? and how to resolve it?
Thanks all! |
It is important to understand how 2D arrays are handled. The data is stored in what is effectively a 1D array, and the code has to know how large the dimension of the second variable is, to do the maths. Hence when you access element 3 2, it knows to multiply the '2', by the size of this dimension, and add this to the '3', to get the location to actually be indexed. Now the problem here is that you are handing the subroutine a pointer, which then means that the compilers 'knowledge' of the array size has been lost inside the routine. You then try to access using a row/column address, and since in the routine, it does not know the size involved, you get the 'too many subscripts' error. If the routine must work with arrays of different sizes, then the answer is to send the size o the second element, as a variable to the routine, and use this to do the arithmetic. This restriction exists generally in C, where in the K&R book, there is the comment: "More generally only the first dimension (subscript) of an array is free; all others have to be specified".
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|