Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 09:24:30 21:24


Login with username, password and session length


Pages: [1]
Print
Author Topic: codevision : passing flash string as parameter  (Read 10599 times)
0 Members and 1 Guest are viewing this topic.
alberto110
Junior Member
**
Offline Offline

Posts: 93

Thank You
-Given: 50
-Receive: 10


« on: May 26, 2013, 06:05:00 18:05 »

Hi
 I wrote a program which has some strings declared with pointers:
Code:
flash char *name_lst_1[3]={"bread","milk","chicken"};
flash char *name_lst_2[3]=....
flash char *name_lst_3[4]=....

how can i send each namelist as parameter to a function ?
« Last Edit: May 26, 2013, 08:25:03 20:25 by alberto110 » Logged
bezobraznic
Newbie
*
Offline Offline

Posts: 20

Thank You
-Given: 83
-Receive: 26


« Reply #1 on: May 26, 2013, 07:03:15 19:03 »

Code:
void lcd_putsf(char flash *str)
{
while(*str) lcd_putchar(*str++);                       
}


lcd_putsf(name_lst_1[x]);
« Last Edit: May 26, 2013, 07:05:33 19:05 by bezobraznic » Logged
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #2 on: May 26, 2013, 07:04:56 19:04 »

Code:
void somefunction(char* cPtr)
{
    Do something here;
}

int main()
{
flash char *name_lst_1[3]={"bread","milk","chicken"};
flash char *name_lst_2[3]=....
flash char *name_lst_2[4]=....

somefunction(name_list_1[0]);
somefunction(name_list_2[1]);
...
}

Btw you've declared 'name_lst_2' two times.
« Last Edit: May 26, 2013, 07:07:18 19:07 by hate » Logged

Regards...
alberto110
Junior Member
**
Offline Offline

Posts: 93

Thank You
-Given: 50
-Receive: 10


« Reply #3 on: May 26, 2013, 08:29:50 20:29 »

Code:
void lcd_putsf(char flash *str)
{
 while(*str) lcd_putchar(*str++);                        
}


lcd_putsf(name_lst_1[x]);
But i want to sent whole array, for  example name_lst_1 with all elements  .
« Last Edit: May 26, 2013, 08:33:47 20:33 by alberto110 » Logged
bezobraznic
Newbie
*
Offline Offline

Posts: 20

Thank You
-Given: 83
-Receive: 26


« Reply #4 on: May 27, 2013, 06:15:08 06:15 »

Code:
for (x=0;x<sizeof(name_lst_1);x++)lcd_putsf(name_lst_1[x]);
Logged
zuisti
Senior Member
****
Offline Offline

Posts: 409

Thank You
-Given: 242
-Receive: 780


« Reply #5 on: May 27, 2013, 11:20:26 11:20 »

Hi alberto110;

First a note:
your pointer arrays (like the name_lst_1[3]) are constant pointer arrays (placed in ROM) but
every item is pointed to a char array (placed in RAM, initialized from ROM).
EDIT:
Sorry, I was wrong, of course 'sam_des' definition of good, see also below, in the next post):
these are  "arrays of pointers placed in RAM and pointing to strings in FLASH"
Thanks.

I do not know the CodeVision, but here is a working MikroC solution (all in ROM to minimize RAM usage):
(const == flash ... I don't know)

Code:
//const pointer array, every item points to a const char array
const char * const txt1[3] = {"First0","First1","First2"};
//an other
const char * const txt2[3] = {"Second0","Second1","Second2"};
//one more
const char * const txt3[4] = {"Third0"," Third1","  Third2","   Third3"};

      //a simple function to use them (parameters: the pointer array and its max-item):

void CstrArrayOut(const char * const arrptr[], char maxitem) {
   const char *cptr;
   char item, ch;

   //a loop to send all strings in the array
   for ( item = 0 ; item < maxitem ; ++item ) {
      //get the actual string's address in rom
      cptr = *(arrptr + item);
      //send a newline before every strings
      UART1_Write(10); //can be omitted (!?)
      UART1_Write(13);
      //a loop to send all chars in the string
      for ( ; ch = *cptr ; ++cptr )
            UART1_Write(ch);
   }
}

    //how to use it (to send each of the four const string pointing via the const txt3 array):
    CstrArrayOut(txt3, 4);
/* the output looks like so:
Third0
 Third1
  Third2
   Third3*/
« Last Edit: May 28, 2013, 08:30:26 08:30 by zuisti » Logged
sam_des
Senior Member
****
Offline Offline

Posts: 253

Thank You
-Given: 124
-Receive: 146


« Reply #6 on: May 27, 2013, 09:59:57 21:59 »

Hello alberto110,

As far as I remember, codevision interprets this,
Code:
flash char *name_lst_1[3]={"bread","milk","chicken"};
as Array of pointers placed in RAM and pointing to strings in FLASH.

If you want both the strings as well as pointers to strings in FLASH you will need,
Code:
flash char * flash name_lst_1[3]={"bread","milk","chicken"};

I don't understand why you want to send whole array to a function.

While individual strings are of different length, pointers to them(whether in RAM or FLASH) will be of same size. You just need to know the address of first element of array and the array length to access entire array from any function. Here is an example of accessing array of strings in flash using pointers in ram,

Code:
void func( flash char* ptr_str_flash, uint8_t a_len )
{
  char str_ram[10];                         // Just for example, size must have space for terminating '\0'

  for( ; a_len; a_len-- ) {
    strcpyf( str_ram, *ptr_str_flash ); // Note str_ram is indeed a pointer, passed as &str_ram[0]
                                                  // Must dereference the ptr_str_flash, since pointer is in RAM pointing
                                                  // to string in FLASH
                                                  // Using special function for CodeVision, RAM<-FLASH
    ptr_str_flash++;                        // point to next string address
  }
}

For any sane C compiler, sending any array as function parameter, inherently sends the pointer to first element of the array and pointer math(++, --) always calculates next element using the correct pointer size.

BTW, as per ANSI C, "const" only means you can't modify value directly. It does not specify where the variable actually is in the memory, since C does not inherently understands different memory types. So most of the compilers provide special attributes like flash, rom, code, eeprom etc.
Also as most c-critics like to point out, you can modify a "const" variable indirectly though a pointer.

Code:
const int i = 10;         // Can init the var once

i = 15;                     // Error ! you can't modify a const

int* ptr = &i;             // Pointer to i
*ptr = 15;                 // Hmm... You modified a const... Ouch...

Hope this helps,
sam_des
Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
alberto110
Junior Member
**
Offline Offline

Posts: 93

Thank You
-Given: 50
-Receive: 10


« Reply #7 on: June 06, 2013, 03:00:37 15:00 »

ok . i found solution  .
simply i used char ** .
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC