|
|
View previous topic :: View next topic |
Author |
Message |
hentropy
Joined: 27 Oct 2004 Posts: 6 Location: Kuala Lumpur, Malaysia
|
How to pass a "const" variable to a function? |
Posted: Sun Dec 26, 2004 12:57 am |
|
|
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
|
|
Posted: Sun Dec 26, 2004 1:15 am |
|
|
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. |
|
|
Ttelmah Guest
|
|
Posted: Sun Dec 26, 2004 10:57 am |
|
|
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
|
|
Posted: Sun Dec 26, 2004 3:15 pm |
|
|
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
|
|
Posted: Mon Dec 27, 2004 7:48 am |
|
|
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. |
|
|
|
|
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
|