CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem passing 2D array

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
micman
Guest







Problem passing 2D array
PostPosted: Mon Apr 05, 2004 6:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 05, 2004 6:38 am     Reply with quote

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







PostPosted: Mon Apr 05, 2004 8:59 am     Reply with quote

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
PostPosted: Mon Apr 05, 2004 2:49 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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