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 support@ccsinfo.com

String Constructors

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



Joined: 13 Aug 2004
Posts: 58
Location: Turkey

View user's profile Send private message Visit poster's website

String Constructors
PostPosted: Tue May 17, 2016 6:09 am     Reply with quote

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: 19447

View user's profile Send private message

PostPosted: Tue May 17, 2016 7:29 am     Reply with quote

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.
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