View previous topic :: View next topic |
Author |
Message |
leto
Joined: 02 Aug 2005 Posts: 14
|
Ponter to Constant |
Posted: Fri Aug 05, 2005 8:39 am |
|
|
Hi,
I want to create a function like this.
void PrintStr( char *StringData )
{
}
When I perform this function: PrintStr("Hello World"), the compiler say:
Attempt to create a pointer to a Constant.
How can i solve this problem?
Thank you.
Leto. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Fri Aug 05, 2005 9:11 am |
|
|
CCS does not allow pointer to const. I use a 2D array
Code: |
const char error_txt[8][16] =
{
"USART overflow", // -0-
"TXBuffer ovrflw", // -1-
"RXBuffer ovrflw", // -2-
"Bad Checksum", // -3-
"", // -4-
"LCD X exceeded", // -5-
"LCD Y exceeded", // -6-
"" // -7-
};
//========================== err_chk ===============================
void ChkErr(void) //print out any errors that occured
{
int8 chk=0;
fprintf(STDERR,"ERROR(%U): %s.\r\n",chk,error_txt[chk]);
}
|
|
|
|
leto
Joined: 02 Aug 2005 Posts: 14
|
|
Posted: Fri Aug 05, 2005 9:39 am |
|
|
Thank you. i'll try as you say. But, I ask me how will the compiler do to compile printf("Hello world").
thanks anyway. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Fri Aug 05, 2005 9:47 am |
|
|
leto wrote: | Thank you. i'll try as you say. But, I ask me how will the compiler do to compile printf("Hello world").
thanks anyway. |
Just use:
Code: | printf("Hello world"); |
_________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
Ttelmah Guest
|
|
Posted: Fri Aug 05, 2005 10:02 am |
|
|
On the PrintStr function, declare a function like this instead:
Code: |
void PrintStr(int chr) {
//Do what you want with a _single_ character here.
//So something like
putc(chr);
}
|
If you then call:
PrintStr("Hello World");
The PrintStr will be called in turn, once for each single character of the output string.
Best Wishes |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Aug 05, 2005 10:38 am |
|
|
If you need to process the entire const string inside a function, then copy it to RAM first and then pass a pointer to the RAM buffer, otherwise ttelmah's suggestion works just fine and is something special that CCS does. |
|
|
leto
Joined: 02 Aug 2005 Posts: 14
|
|
Posted: Fri Aug 05, 2005 11:02 am |
|
|
thank you again.
Im trying to improve the GlcdText57 function from graphics.c driver.
It receive a char pointer with the string to print, then it print each byte.
But, you must to declare the buffer previously.
e.g
char buffer[]="Hello Word";
GldText57( row, col, buffer, size, ..)
each time you want to print something you must to copy the String to buffer.
Sprintf(buffer,"string to print");
GldText57( row, col, buffer, size, ..);
I think Ttelmah's suggestion would must be better to dont spend enough memory. may be a little slower. What do you think?
Regards,
Leto. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Aug 05, 2005 11:07 am |
|
|
If you are only going to print const strings then ttelmah suggestion works if you dont need to process the entire string at once.
However, if you are going to print strings that are "made on the fly" then you might be better off copying them to ram. |
|
|
|