[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[avr-gcc-list] sprintf
From: |
David VanHorn |
Subject: |
[avr-gcc-list] sprintf |
Date: |
Thu, 26 Feb 2009 19:57:15 -0500 |
So I was using sprintf to create some data to send to the LCD, by way of lcd_puts.
I had a pair of arrays, A_String[17], and B_String[17] which I filled using two loops.
I hit some problems when I tried to consolidate this into one loop.
The symptom was that the second string wouldn't display, but it would if I had the two routines in separate loops.
I'm creating a simple bar graph, Data_A and Data_B have some value that is roughly between 0 and 16* the Step value.
for (i=0; i<16; i++)
{
if ((Step * i) < Data_A) {
sprintf((A_String+i), "%c", 0xFF);
} else {
sprintf((A_String+i), "%c", 0x20);
}
if ((Step * i) < Data_B) {
sprintf((B_String+i), "%c", 0xFF);
} else {
sprintf((B_String+i), "%c", 0x20);
}
}
lcd_home();
lcd_gotoxy(0,0);
lcd_puts(A_String); // String must be null terminated.
lcd_gotoxy(0,1);
lcd_puts(B_String);
In the above implementation, B_String would not show up on the display, but A_String would..
Below is the version that worked, the only difference is that the A string is completed before starting on the B string.
for (i=0; i<16; i++)
{
if ((Step * i) < Data_A) {
sprintf((A_String+i), "%c", 0xFF);
} else {
sprintf((A_String+i), "%c", 0x20);
}
for (i=0; i<16; i++)
{
if ((Step * i) < Data_B) {
sprintf((B_String+i), "%c", 0xFF);
} else {
sprintf((B_String+i), "%c", 0x20);
}
}
It turned out that I had neglected to allow for the terminating null in the strings, but this raises two questions for me.
1: Why doesn't sprintf((A_String+16), "%c", 0x00); drop a null into the last element of A_String.
2: Why does the separate loops case work at all?
I solved my problem by inserting the null with
memset((A_String+16), 0, 1); and the same for B_String.
- [avr-gcc-list] sprintf,
David VanHorn <=
- Re: [avr-gcc-list] sprintf, Dave Hylands, 2009/02/26
- Re: [avr-gcc-list] sprintf, David VanHorn, 2009/02/26
- [avr-gcc-list] Re: sprintf, David Brown, 2009/02/27
- Re: [avr-gcc-list] Re: sprintf, Vincent Trouilliez, 2009/02/27
- Re: [avr-gcc-list] Re: sprintf, David VanHorn, 2009/02/27
- Re: [avr-gcc-list] Re: sprintf, Vincent Trouilliez, 2009/02/27
- Re: [avr-gcc-list] Re: sprintf, David VanHorn, 2009/02/27
- Re: [avr-gcc-list] Re: sprintf, David Kelly, 2009/02/27
- Re: [avr-gcc-list] Re: sprintf, Russell Shaw, 2009/02/28
- [avr-gcc-list] Re: sprintf, David Brown, 2009/02/28