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 CCS Technical Support

Compiler issue with declaring strings

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







Compiler issue with declaring strings
PostPosted: Mon Sep 19, 2005 1:01 am     Reply with quote

Hi all,

I have this strange issue with declaring a string.
All C compilers except CCS work. It is a C compliant declaration.

char *string="This will give an error";

This too:
char *string[1]={ "this also gives","an Error" };

Does anyone knows why or how to solve this?
I know there are other ways to create strings, but this should work.

Regards,


Evert
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 19, 2005 1:39 am     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=21354
http://www.ccsinfo.com/forum/viewtopic.php?t=23167
http://www.ccsinfo.com/forum/viewtopic.php?t=23910
ckielstra



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

View user's profile Send private message

PostPosted: Mon Sep 19, 2005 1:44 am     Reply with quote

From page 1 of the compiler manual:
Quote:
The compilers have some limitations when compared to a more traditional C
compiler. The hardware limitations make many traditional C compilers
ineffective. As an example of the limitations, the compilers will not permit
pointers to constant arrays. This is due to the separate code/data segments in
the PICmicroŽ MCU hardware and the inability to treat ROM areas as data.

Your problem is caused by the PIC processors having seperate memory areas for data and program memory. The constant data string must be stored in ROM program memory (or it would be lost at power down) but in CCS a char pointer always points to RAM. The C language offers no default method for solving these types of memory conflicts and every compiler vendor provides it's own solution (often in a seperate linker/locator module).


There are a few solutions to work around the pointer problem, the most common is to first copy data from ROM to RAM and from then on everything works as usual.
Code:
char string[25];
strcpy(string, "This will work.");


A second very powerfull workaround can be found on page 71 of the CCS manual:
Quote:
A (non-standard) feature has been added to the compiler to help get around the
problems created by the fact that pointers cannot be created to constant strings.
A function that has one CHAR parameter will accept a constant string where it is
called. The compiler will generate a loop that will call the function once for each
character in the string.
Example:
void lcd_putc(char c ) {
...
}
lcd_putc ("Hi There.");
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