View previous topic :: View next topic |
Author |
Message |
curroortuno
Joined: 18 Apr 2008 Posts: 1
|
problem with printf and const |
Posted: Fri Apr 18, 2008 5:13 am |
|
|
Hi,
I use CCS Compiler v4.065 and I have the following code:
Code: |
const otra[3][* ]={
"Weight=%u Kg"
"Height= %u cm"
"Age= %u years"
};
void main(){
// Init LCD
....
// I like in LCD: "Height= 185 cm"
printf(lcd_putc,otra[1], 185);
|
But, when I compile, the message than I see in LCD is:
"Weight= %u Kg Height=185 cm"
Why does LCD show the first message? How do I solve this problem?
Thanks. |
|
|
Matro Guest
|
|
Posted: Fri Apr 18, 2008 5:33 am |
|
|
Try that :
Code: |
char const otra[3][*]={
"Weight=%u Kg",
"Height= %u cm",
"Age= %u years"
};
void main(){
// Init LCD
....
// I like in LCD: "Height= 185 cm"
printf(lcd_putc,otra[1], 185);
|
Matro |
|
|
Guest
|
|
Posted: Fri Apr 18, 2008 5:40 am |
|
|
Excuse me, this is my code:
Code: |
const otra[3][*]={
"Weight=%u Kg"
"Height= %u cm"
"Age= %u years"
};
void main(){
// Init LCD
....
// I like in LCD: "Height= 185 cm"
printf(lcd_putc,otra[1], 185);
|
It is same that your code but LCD don't show "Height= 185 cm".
Thanks |
|
|
Matro Guest
|
|
Posted: Fri Apr 18, 2008 6:26 am |
|
|
This is not the same as mine.
I added type to constant definition and commas at the end of each "cell" of array definition (except last).
Copy paste my code, try it and tell us about.
Matro. |
|
|
Guest
|
|
Posted: Fri Apr 18, 2008 7:29 am |
|
|
Excuse me, again.
I have copied your code but LCD shows the same message:
"Weight=%u Kg Height= 185 cm"
LCD shows the first string though I write "otra[1]" in the printf.
Thanks. |
|
|
Matro Guest
|
|
Posted: Fri Apr 18, 2008 8:19 am |
|
|
And with this solution?
Code: |
char* const otra[3]={
"Weight=%u Kg",
"Height= %u cm",
"Age= %u years"
};
void main(){
// Init LCD
....
// I like in LCD: "Height= 185 cm"
printf(lcd_putc,otra[1], 185);
|
Matro |
|
|
Matro Guest
|
|
Posted: Fri Apr 18, 2008 8:42 am |
|
|
Matro wrote: | And with this solution?
Code: |
char* const otra[3]={
"Weight=%u Kg",
"Height= %u cm",
"Age= %u years"
};
void main(){
// Init LCD
....
// I like in LCD: "Height= 185 cm"
printf(lcd_putc,otra[1], 185);
|
Matro |
It seems that this is not supported by CCS...
Maybe you can try all in a single line :
Code: |
char const otra[3][*]={"Weight=%u Kg","Height= %u cm","Age= %u years"};
|
In case of a compiler bug.
Matro |
|
|
Guest
|
|
Posted: Fri Apr 18, 2008 9:02 am |
|
|
Thank you Matro, but these codes don't solve my problem. Have you some idea more? |
|
|
Matro Guest
|
|
Posted: Fri Apr 18, 2008 9:21 am |
|
|
Anonymous wrote: | Thank you Matro, but these codes don't solve my problem. Have you some idea more? |
I've in the mind that this will never work...
CCS reference manual says that an array defined as "const" can't be accessed through a pointer. And that's what you are trying to do.
I think the best is to pass strings in RAM, either by using strcpy() function, or by using directive
Code: |
#device PASS_STRINGS = IN_RAM
|
Matro. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 18, 2008 11:57 am |
|
|
Quote: |
char const otra[3][*]={
"Weight=%u Kg",
"Height= %u cm",
"Age= %u years"
};
void main(){
// Init LCD
....
printf(lcd_putc, otra[1], 185);
the message than I see in LCD is:
"Weight= %u Kg Height=185 cm"
|
You need to understand something about CCS. It's intended for small
embedded microcontroller chips, so because of that, the compiled code
is designed to be small.
Your example is based on the belief that CCS has a large run-time
module (such as MSCRT.DLL) that contains the entire printf parsing
mechanism. But it does not.
You are trying to pass the format string to printf at run-time. But CCS
doesn't parse the format string at run-time. It parses it at compile-time,
and creates a small, compact, specific code to handle the "%u" only.
If you also put in "%f", then it creates code for that too. But it's all
created at compile-time. It's hard-coded. |
|
|
|