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 printf and const

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



Joined: 18 Apr 2008
Posts: 1

View user's profile Send private message

problem with printf and const
PostPosted: Fri Apr 18, 2008 5:13 am     Reply with quote

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







PostPosted: Fri Apr 18, 2008 5:33 am     Reply with quote

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








PostPosted: Fri Apr 18, 2008 5:40 am     Reply with quote

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







PostPosted: Fri Apr 18, 2008 6:26 am     Reply with quote

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








PostPosted: Fri Apr 18, 2008 7:29 am     Reply with quote

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







PostPosted: Fri Apr 18, 2008 8:19 am     Reply with quote

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







PostPosted: Fri Apr 18, 2008 8:42 am     Reply with quote

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... Sad
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








PostPosted: Fri Apr 18, 2008 9:02 am     Reply with quote

Thank you Matro, but these codes don't solve my problem. Have you some idea more?
Matro
Guest







PostPosted: Fri Apr 18, 2008 9:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 18, 2008 11:57 am     Reply with quote

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.
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