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

Problem with constant strings
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

Problem with constant strings
PostPosted: Thu Jun 14, 2012 10:56 am     Reply with quote

Hi all,

I'm having some trouble with constant strings, I'm trying the c code as found on this page:

http://www.ccsinfo.com/content.php?page=compiler-features#pconst

I have put the strings definition in the .c file just before the main() function and the loop into the main() function. My only change has been to make printf into fprintf.

Code:

const char *strings[] =
{
   "HELLO",
   "WORLD",
   "CONST",
   "STRINGS"
};

const char *ptr;
   int i;
   while (i = 0; i < (sizeof(strings) / sizeof(const char *)); i++)
   {
      ptr = strings[i];
      fprintf(RS232A, "%s", ptr);
   }


To start with this doesn't compile, the declaration of the pointer seems right to me (pointer to constant char*) but it expects an = after the declaration like the pointer is constant (it shouldn't be).

If you change the pointer to just char *ptr; then the loop fails to compile. To simplify it all I tried this:

Code:
const char *ptr = strings[2];
   fprintf(RS232A, "%s", ptr);

But I just get classic bad ptr gibberish.

All suggestions would be most welcome, I am clearly misunderstanding something!

Thanks very much

Ed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 14, 2012 11:18 am     Reply with quote

See these example programs:
http://www.ccsinfo.com/forum/viewtopic.php?t=30611&start=6
http://www.ccsinfo.com/forum/viewtopic.php?t=30610&start=1
Ttelmah



Joined: 11 Mar 2010
Posts: 19326

View user's profile Send private message

PostPosted: Thu Jun 14, 2012 11:24 am     Reply with quote

Compiler version, chip?????.

Change the word 'const', to 'rom'.

CCS has two types of rom constant string. The older type (which is that standard), by default uses the 'const' keyword. The later type, uses the rom keyword. The earlier type is simpler to handle, _but does not support pointers_. This is the only type available on some PIC's (early PIC16's etc.). Here there is no hardware support for this, and const arrays are 'simulated' by generating a program that returns the characters when called with an index. If your chip is an old one, then this may apply.

You can swap so that 'const' behaves as 'rom', at a cost in program size, with the keyline '#DEVICE CONST=ROM'.

There is also a third way of handling 'const'. In ANSI C 'const' does not imply storage in ROM, instead just meaning the bytes are protected from writing. You can switch to this mode with the keyline '#DEVICE PASS_STRINGS=IN_RAM'. This costs a significant amount of RAM, copying the strings to RAM before use, but then allowing const to support pointers.
I'd suspect the code you are using has this option.

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 14, 2012 11:40 am     Reply with quote

Example of using a rom pointer:
http://www.ccsinfo.com/forum/viewtopic.php?t=45132&start=3
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

PostPosted: Fri Jun 15, 2012 5:33 am     Reply with quote

hi guys,

Thanks for the responses, I had a look through the stuff and revised my code to this:
Code:

char rom *strings[] =
{
   "HE",
   "WO",
   "CON",
   "STRINGS"
};

fprintf(RS232A, "Ed: %s", strings[2]);


This still doesn't work correctly and neither does the simpler:

Code:
char rom string[] = "hello";
char rom *ptr; // ptr is stored in RAM, is a pointer to ROM
ptr = string;
fprintf(RS232A, "Ed: %s", ptr); 


I'm using compiler 4.132 for an 18F87K22. It seems like I missing something fundamental here I still get streams of incorrect characters from the fprintf call. I haven't defined any of the copy in ram compiler settings or anything like that.

Cheers

ed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 15, 2012 3:59 pm     Reply with quote

I think there may be a problem with rom pointers that is version
dependent. I don't have a work-around for it right now. I'm going
to investigate it some more.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 17, 2012 2:52 pm     Reply with quote

Rather than me spending more time trying to make 'rom' work, I just
made some semblance of your original code work. In MPLAB simulator,
the code shown below displays the following:
Quote:

HELLO
WORLD
CONST
STRINGS

This is with compiler vs. 4.132.
Code:

#include <18F87K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

const char strings[4][8] =
{
"HELLO",
"WORLD",
"CONST",
"STRINGS"
};

//======================================
void main(void)
{
int8 i;

for(i=0; i < 4; i++)
   {
    printf("%s \r", strings[i]);
   }

while(1);
}
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

PostPosted: Mon Jun 18, 2012 1:38 am     Reply with quote

Hi PCMProgrammer,

Thanks for having a look at this, I tried your code and it works for me, although of course you're allocating the longest string length to every string even when they don't need the space. I tried this:

Code:
const char strings[4][] =
{
"HELLO",
"WORLD",
"CONST",
"STRINGS"
};

fprintf(RS232A, "Ed: %s", strings[2]);

But I get string truncated warnings and it doesn't work.

I would also expect this to work:
Code:
const char *string2 = strings[2];

But I get error 27 must evaluate to a constant.

My actual code at the moment looks like:
Code:
char_t va_state_str0[] = "Closed";
   char_t va_state_str1[] = "Partly Open";
   char_t va_state_str2[] = "Open";
   char_t* va_state_str[] = { va_state_str0, va_state_str1, va_state_str2 };


And as I have no run out of RAM it would be nice to move all this stuff to ROM. Do you think I should report this as a bug?

Thanks

Ed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 18, 2012 3:43 pm     Reply with quote

Variable length constant strings used to work. I think it's broken for vs.
4.132. I made a demo program for it here, and it worked in vs. 4.104
but it doesn't work now:
http://www.ccsinfo.com/forum/viewtopic.php?t=41353
I just did some more testing, and it works up through vs. 4.130.
Then starting with 4.131, it fails. It's the old problem of lack of
regression testing with CCS.


With the rom ptr example that I linked to earlier, it works up through
vs. 4.129. But with 4.130 and later it fails. So CCS has been doing
some changes and either they changed the syntax and didn't tell us,
or they broke those features.

If you want to report these problems, go ahead. I reported a bug in
memset() on May 27, and never got even an acknowledgment response.
I also asked about the CRC used algorithm in the hex file on June 7, and
never got a reply. So I don't know what's going on.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Jun 19, 2012 12:48 am     Reply with quote

Besides the same old story of undocumented arbitrary changes to the CCS compiler, it's worth to think
about how variable length strings are implemented and why we expect them to work.

Technically, an array definition is just a rule to calculate object addresses. Obviously, it can't work
to access an array of variable length strings. They need an additional means like a table of string
addresses "hidden" behind the [number_of_strings][] syntax, as it's done in the Microchip Cxx compilers.

I didn't yet check the behaviour with different compiler versions. I understood that according to CCS
the [*] syntax should be used for variable length strings. This suggests, that the working of [] in previous
versions would be an unspecified side effect.
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

PostPosted: Tue Jun 19, 2012 6:34 am     Reply with quote

Variable length strings would be great but I can guess I can do without, shouldn't this work?

Code:
const char string_a[6] = "hello";
const char string_b[6] = "world";
const char *strings[2] = {string_a, string_b};


I get an error 27 must evaluate to a constant, but string a and b are constant! Maybe my syntax is bad, what I think I want is, an array in ram of pointers to const strings. So I can then use the syntax:

Code:
fprintf(RS232A, "ed: %s\r", strings[1]);


Does that seem reasonable?

Cheers

ed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 19, 2012 2:43 pm     Reply with quote

Yes, but traditionally there are no pointers to constant strings, and as I
have shown, the work-arounds (like 'rom') don't work in the current
compiler version. See Ttelmah's comments in these threads regarding
pointers to constant strings:
http://www.ccsinfo.com/forum/viewtopic.php?t=48198&start=2
http://www.ccsinfo.com/forum/viewtopic.php?t=42070&start=1
http://www.ccsinfo.com/forum/viewtopic.php?t=34837
http://www.ccsinfo.com/forum/viewtopic.php?t=40071
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

PostPosted: Thu Jun 21, 2012 8:56 am     Reply with quote

I can confirm the problem shown by my code is still present in 4.133.

Cheers

Ed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 21, 2012 1:05 pm     Reply with quote

Did you email CCS support about the problems with rom pointers and
variable length strings in the last few versions ? Or do you want me to do it ?
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

PostPosted: Fri Jun 22, 2012 2:06 am     Reply with quote

Sorry I haven't yet, I'll do it today. Perhaps if we both emailed about the same issue and included the link to this thread we might get a better response.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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