View previous topic :: View next topic |
Author |
Message |
davidd
Joined: 30 Sep 2010 Posts: 38
|
Running string loop with external pointer |
Posted: Wed Feb 10, 2016 5:45 pm |
|
|
Hello All,
I have a function that takes advantage of the loop mode, it takes a direct string between quotes and sets up a loop to process the entire string.
but If I send it the pointer, it only processes the first character. Anyone know of a way around this?
Ultimately I want to use this function within a function, and pass the string pointer to it...
SEE CODE
Code: |
//global
char txt[20];
show_string("test");
//processes entire string
//1 character at a time
//decodes bitmap -- sends to screen
strcpy(txt, "test");
show_string(*txt);
//processes first character only
ultimately
int1 show_option(char *txt){
// show_string(txt);
// wait for button press{}
// button press = return true;
// timeout = return false;
// }
|
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 10, 2016 5:52 pm |
|
|
Quote: |
char txt[20];
show_string(*txt);
but If I send it the pointer
|
That's not how you pass the address of an array to a function.
Research it in a book on C or using the net. |
|
|
davidd
Joined: 30 Sep 2010 Posts: 38
|
|
Posted: Thu Feb 11, 2016 8:38 am |
|
|
Thanks PCM Programmer.
Indeed the "address of" operator is used here:
Code: |
char txt[20];
show_string(&txt);
|
What im ultimately trying to accomplish is
Code: |
int8 get_menu(int8 level, char txt_ROM){
char txt_RAM[15];
strcpy(txt_RAM, txt_ROM);
show_string(&txt_RAM);
//wait for button press... etc
}
where the function call would be...
choice = get_menu(1, "option 1");
|
so I guess my next question is how to pass the string in ROM as a parameter?
I think I have to do a strcpy before each time I use the function.
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Feb 11, 2016 9:43 am |
|
|
You don't need the &.
In C, the name of an array, without any index, _is_ it's address. So you can just use the array name.
The compiler (fortunately) is smart enough to know if you ask for the address of this address, it should just return the address, but on a few rare occasions, with complex types, this can fail. So safer to just use the name.
You have two choices on an array in ROM. The first is just to copy them to RAM as you do. The second is to use the compiler option PASS_STRINGS=IN_RAM
Using this, if you hand a pointer to a const string to a function, the compiler will automatically 'virtualise' it into RAM. It uses a small buffer to do this and the array is then treated as if it did exist in RAM.
Downside is this is fractionally less efficient, bringing a small (very small normally) time penalty compared to the single copy. |
|
|
davidd
Joined: 30 Sep 2010 Posts: 38
|
|
Posted: Thu Feb 11, 2016 10:42 am |
|
|
After trying many ways, I don't think it is possible to activate the loop mode UNLESS the actual string is hardcoded into the function like this:
Code: |
void str_function(char str);
str_function("hello world"){
// process 1 character at a time
}
|
Following will not work even though string is copied to RAM:
Code: |
char string[15];
strcpy(string, "example");
str_function(&string);
|
no luck with
Code: |
#device PASS_STRINGS=IN_RAM
|
either
Thanks |
|
|
ronaldoklais
Joined: 18 Dec 2012 Posts: 13
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Feb 11, 2016 11:12 am |
|
|
You could use ROM pointers, but your problem is with your C syntax:
void str_function(char str);
Is saying that str_function is receiving a _single character_. Not a pointer. This needs to be:
void str_function(char *str);
Or
void str_function(char str[]);
The 'hardcode' works, because in CCS, if you have a function that is written to accept a single character. and you call this with:
function("A const string")
Then the function is automatically called multiple times for each character in the string.
You are using &, and * in the wrong places, initially having the * in the call, not the declaration, and still having the & in the call.
You need to write your function, so that it is actually coded to receive a string. Get a basic C textbook, or look at some examples online (or read the examples)... |
|
|
davidd
Joined: 30 Sep 2010 Posts: 38
|
|
Posted: Thu Feb 11, 2016 11:57 am |
|
|
Quote: |
void str_function(char str);
Is saying that str_function is receiving a _single character_. Not a pointer. This needs to be:
void str_function(char *str);
|
Ttelmah thanks for your response.
The issue is if the * is added to str, the looping function no longer works.
in its original form, void str_function(char str);
when called via str_function("example");
will process "example" one char at a time,
However, void str_function(char *str); breaks the looping function.
So I don't see how to use the looping function via a pointer.
what I will end up doing is creating a function with switch case (menu option)
Code: |
void menu_txt(int8 menu_option){
switch(menu_option){
case 1: show_string("first option"); break;
case 2: show_string("second_option"); break;
case 3: ...
//etc
return;
}
|
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Feb 11, 2016 1:50 pm |
|
|
This really is basic C. You need to get a C textbook.
With a pointer:
Code: |
//Suitable processor header
void display_something(char str[])
{
while (*(str) != '\0')
{
//at this point *str is a character in the source string
//do what you want with it
str++; //move to next character
}
}
void main(void)
{
char buffer[] = "Demo string";
//generate the string any way you want
display_something(buffer);
while(TRUE)
;
}
|
You have to use the pointer to walk through the string till you see the string terminator (\0). |
|
|
davidd
Joined: 30 Sep 2010 Posts: 38
|
|
Posted: Thu Feb 11, 2016 3:39 pm |
|
|
Ttelmah thanks for your response.
yes, I realize I could parse it too.
thanks for your help! |
|
|
|