scanan
Joined: 13 Aug 2004 Posts: 59 Location: Turkey
|
String Constructors |
Posted: Tue May 17, 2016 6:09 am |
|
|
Hello,
I would like to know how I can replace the String constructor found in arduino programs into CCS C any comments ?
some examples below
Code: |
void push(String* str, String s) {
// loop for find space that can be collect the data
for (int i = 0; i < sizeof(*str); i++) {
if (str[i].equals("")) { //found space
str[i] = s; // store in that slot
break; // stop loop
}
}
}
String pop(String* str) {
if (str[0].equals("")) { // empty data
return ""; // return empty string
}
// loop for find lastest data
for (int i = 0; i < sizeof(*str) - 1; i++) {
if (str[i + 1].equals("")) { // found next slot that is empty space
String s = str[i]; // get lastest data into s String variable
str[i] = ""; // clear that slot into empty space
return s; // return data
}
}
}
|
_________________ Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com
Do whatever you do with amateur spirit -
But always feel professional. |
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue May 17, 2016 7:29 am |
|
|
A string in C (generic, not just CCS), does not have a 'size' when passed to a function. There is _not_ actually a 'string' type in C. A string in C, is just an array of characters. You have to use strlen to find the size of a string, not sizeof. The PIC does not have a RAM stack, so this has to be emulated. CCS has the standard C memory handling functions, so you can even emulate a dynamic stack, or use a fixed size one.
There are standard C libraries to do this type of thing.
So:
<http://www.cprograms.in/Stack/string-stack.html>
Shows push and pop, on a fixed 80byte string stack.
Look at this thread as well:
<http://www.ccsinfo.com/forum/viewtopic.php?t=55157> for why you should really only specify the elements per row in the calls. |
|