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

Pointer to 2D array in function call

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



Joined: 25 Jan 2023
Posts: 21

View user's profile Send private message

Pointer to 2D array in function call
PostPosted: Wed Jan 25, 2023 12:34 pm     Reply with quote

Hello, pls, how to use pointer to 2D array stored in ROM in function call ?
When I used Font array in function as static 2D array, it works well and fast, but I'd like to use various 2D arrays in function call. Thanks a lot.
MCU: 18K26K22

Edit: so this one works, but I have to insert size of 2D array in function call:

Code:

rom u8 Ravie16x16[16][33] = { 
        0x07, 0x00, 0x00, 0x......
        0x0D, 0x01.............

void Nokia_BigUni(rom u8 *FONT[16][33],u8 px,u8 py, int8 c) {
   u8 char_pos,i;
   u8 CharLen;   
   
   if(c < 46) c=46;
   if(c > 57) c=57;
   
   char_pos=(c-46);
   CharLen = *FONT[char_pos][0];


Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 1:52 am     Reply with quote

That is standard and correct.
In C, an array passed to a function, does not 'know' anything about it's
dimensions. It 'decays' to just a pointer. You have a series of choices:
1) Pass as a single dimensional array of pointers. Each pointer then
points to the second list of elements.
2) Pass as a fixed size array known at compile time (this is what you
are now doing). Technically it is only the last dimension that needs to
be known.
3) Pass as a single dimensional array, and pass the size of the elements
as another variable. Then handle the addressing yourself.

Get a standard C textbook. It'll explain this.
pet007



Joined: 25 Jan 2023
Posts: 21

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 1:55 am     Reply with quote

Thanks, yes, now it's clear for me.
pet007



Joined: 25 Jan 2023
Posts: 21

View user's profile Send private message

PostPosted: Thu Jan 26, 2023 1:53 pm     Reply with quote

So this code below works, as Ttelmah said:
('cols' is length of each "row" in 2D array, listed in 1st post.)

Code:

void Nokia_BigCh(rom u8 *FONT, u8 Cols, u8 px,u8 py, int8 c) {
   u8 i;             
   u16 char_pos;
   u8 CharLen;   
   
   if(c < 46) c=46;
   if(c > 57) c=57;
   
     char_pos=(u16)(c-46)*(u16)Cols;   
      CharLen = FONT[char_pos][0];     
    Nokia_cmd(0x22);
    Nokia_gotoXY(px,py);
   
   CharLen = CharLen << 1; 
   for(i=1;i < CharLen;i=i+2) { 
       Nokia_write(FONT[char_pos][i]);  Nokia_write(FONT[char_pos][i+1]);
....


For simpler call I am using this definition also:
Code:


#define Nokia_RevieNum(x,y,c)  Nokia_BigCh(Ravie16x16,33,x,y,c+46)


so in main.c :

Code:

    Nokia_RevieNum(pos_x, pos_y, digit);   // digit: 0-8

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