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

How to pass a "const" variable to a function?

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



Joined: 27 Oct 2004
Posts: 6
Location: Kuala Lumpur, Malaysia

View user's profile Send private message

How to pass a "const" variable to a function?
PostPosted: Sun Dec 26, 2004 12:57 am     Reply with quote

Dear Guys,

Can you guys please help me on this? I am writing code to transmit some text to a printer.

However, the text which is pre-defined in my source file seems to take too much space, and the compiler reports the insufficient RAM problem.

So, i declared the global variables as type const, hoping that the compiler will put it into ROM, since I suspect i still have plenty of that left.


Code:
char const TicketStr8[] = "First string.\n"; 
char const TicketStr9[] = "Example second string.\n";
char const TicketStr10[] =
{
   '\n',
   ESC,  0x64, 0x04,   // line
   GS, 0x56, 0x01   // full cut
};


void PrintText(  char *str, int len, int deviceid )
{
   // ... some code
}

void Event_TMAC_BTN( void )
{
   if( Counter == 32 )
   {
      PrintText( TicketStr8, sizeof(TicketStr8), 2 );   // Line 317 - Error
      PrintText( TicketStr9, sizeof(TicketStr9), 2 );   // Line 318 - Error
      PrintText( TicketStr10, sizeof(TicketStr10), 2 );   // Line 319 - Error   
   }
   else
   {
      // ...
   }
}



However, compiling this code above produces the following error:

Code:
Error[22]   G-030Q.C 317 : Bad expression syntax



I've tried to redeclare the function as

Code:
void PrintText(  char const *str, int len, int deviceid )


but it doesn't help either. The same error message also comes up.


I would really appreciate your help. Thank you & kind regards.


My compiler: PCM version 3.157
hentropy



Joined: 27 Oct 2004
Posts: 6
Location: Kuala Lumpur, Malaysia

View user's profile Send private message

PostPosted: Sun Dec 26, 2004 1:15 am     Reply with quote

By the way, Merry Christmas to all christians in this forum, and may the Lord bless all of you who selflessly help the ones in need here. Very Happy
Ttelmah
Guest







PostPosted: Sun Dec 26, 2004 10:57 am     Reply with quote

The key here, is that you cannot have a pointer to a constant string. Hence you cannot pass the variable as shown. There are two options. The first is to have a single RAM variable large enough to hold any of the strings, and then copy the strings in turn to this, and call the function using this. The alternative, (which depends on how the PrintText' function actually behaves), is to rewrite this to receive the characters 'one at a time', as integers.
If (for instance), you have a routine that outputs a character as you want, and define it as:
Code:

void PrintText(  int8 achr, int deviceid )
{
   // ... some code
}


and then call this with:

PrintText("A constant string",ID);

The actual function will be repeatedly called, with each character in the source string in turn.
This is a 'shortcut', to allow easier handling of the constant strings, given the lack of pointer ability, and often allows what is needed to be done.

Best Wishes (and very festive time to all).
hentropy



Joined: 27 Oct 2004
Posts: 6
Location: Kuala Lumpur, Malaysia

View user's profile Send private message

PostPosted: Sun Dec 26, 2004 3:15 pm     Reply with quote

Thanks a lot Ttelmah, the shortcut method is really something new for me. Will try it out.

Regarding the fact that we cannot have a pointer to a const string, is this limitation only for PCM or for all C compilers in general?

Thanks again.
Ttelmah
Guest







PostPosted: Mon Dec 27, 2004 7:48 am     Reply with quote

hentropy wrote:
Thanks a lot Ttelmah, the shortcut method is really something new for me. Will try it out.

Regarding the fact that we cannot have a pointer to a const string, is this limitation only for PCM or for all C compilers in general?

Thanks again.

It is a limitation in CCS. However on the other compilers that do allow this, it requires a lot of code overhead to provide on the PIC, and still has problems on many chips. The 'heart' of the limitation is the memory architecture of the PIC. The seperate RAM, and ROM data spaces, and the lack of a function to directly access these spaces, makes this very hard to implement. This limitation has been slightly 'bypassed' on the latter flash PICs (whch do have a function to 'read' a specified address in ROM), and I think a lot of people are perhaps suprised that on these chips, CCS has not provided at least some part of the ability...

Best Wishes, and 'Happy New year' wishes to all.
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