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

A simple question about strings in CCS!

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



Joined: 26 Mar 2005
Posts: 10

View user's profile Send private message Send e-mail ICQ Number

A simple question about strings in CCS!
PostPosted: Tue Jun 14, 2005 5:29 pm     Reply with quote

I'm using CCS since an year and more...anyways, I've tryed many times to run a simple code like this:
Code:

char* Hello()
{return "Hello"}
...
priintf("%S",Hello());

Never works for me! Why? Another test done by me:
Code:

strcpy(s,Hello());
....
priintf("%S",Hello());


Also not working? I have so many programs behind my back that needed to be written in the way described above, but I just try to round writing that way. Can someone explain why is this code not working and how should I write it Cool
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Tue Jun 14, 2005 5:55 pm     Reply with quote

CCS does not support pointers to constant strings.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jun 15, 2005 2:00 am     Reply with quote

First some theory:
The processors that are used in a PC have a Von Neuman hardware architecture with a single memory space. The PIC processor has a Harvard hardware architecture with seperate data and instruction memories, this allows for faster program execution because instructions and data can be fetched simultaneously. Using Google you can find more details over the differences between Harvard and Von Neumann architectures.

Programs that use constant data, like fixed text strings, need a way to store this data permanently, i.e. in ROM instead of in RAM. This is often resolved by storing the constant data in program memory (flash). In order to get access to the stored data you need to execute some special instructions to transfer data from instruction memory to data memory. The need for these extra instructions is why a direct pointer to constant data in instruction memory is not working.

The workaround is to first copy contstant strings to a RAM buffer and then all pointer operations work as expected.

Code:
  char SearchString[10]; // This must be big enough including the NULL terminator

strcpy(SearchString, "OK");
Predator_MF



Joined: 26 Mar 2005
Posts: 10

View user's profile Send private message Send e-mail ICQ Number

PostPosted: Wed Jun 15, 2005 2:13 am     Reply with quote

@ckielstra, thanks alot. I know what about PIC architecture and so on. I told erlier that I've written many programs working with PIC12, PIC16 and PIC18 written with CCS. My question is more than simple:
Code:
char* Hello();
{return "Hello"};

This code works on PC, not on PIC (CCS). How should I write my function to output a string? Using a global variable that is with defined length? (This is the way I was doing it for a long time)
Ttelmah
Guest







PostPosted: Wed Jun 15, 2005 3:05 am     Reply with quote

Code:

char* Hello()  {
   //Assign RAM storage to hold the string
   static char store[8];
   //copy string from ROM to RAM
   strcpy(store,"Hello");
   return store;
}
...
priintf("%S",Hello());


Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jun 15, 2005 10:20 am     Reply with quote

The static variable in Ttelmah's example is better than using a global variable because now it is clear to which function the variable belongs, limiting the scope. Disadvantage is that this variable is permanently allocating valuable RAM space.

Another solution I sometimes use is to have the calling function allocate RAM on the stack.
Code:
void function1()
{
  int8 Store[8];

  Hello(Store);
  printf("%S", Store);
}

void Hello(int8 *Buffer)
{
  //copy string from ROM to RAM
  strcpy(Buffer,"Hello");
}
Advantage of this solution is that you are not permanently allocating RAM. Disadvantage is that the pointer to the buffer is invalid as soon as you leave function1.
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