<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Re: Sprintf error</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/43046/re-sprintf-error</link><description> 
I have written a routine to update an array using sprintf
statement but after the execution the array doesnt seem to update. I
am clueless where i have made the mistake. Can anybody help me? The
program is given below. 

 
 
#include&amp;lt;reg669.h&amp;gt; 
#include</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/150289?ContentTypeID=1</link><pubDate>Tue, 18 Mar 2008 03:00:02 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:58ebdf0a-bf47-4991-93fe-32923523e393</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
Can&amp;#39;t you just use something like:&lt;/p&gt;

&lt;pre&gt;
void display_a_value( unsigned char value_id )
{
   switch( value_id )
   {
      case 0: printf( &amp;quot;this value is: %u&amp;quot; this_value );
              break;

      case 1: printf( &amp;quot;that value is: %u&amp;quot; that_value );
              break;

      case 2: printf( &amp;quot;the other value is: %u&amp;quot; the_other_value );
              break;

      etc...
   }
}
&lt;/pre&gt;

&lt;p&gt;
Of course, you&amp;#39;d need some meaningful identifiers, and there&amp;#39;s
room for some optimisation - but that&amp;#39;s the basic idea.&lt;/p&gt;

&lt;p&gt;
Always start with the basics; optimise later!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/149854?ContentTypeID=1</link><pubDate>Tue, 18 Mar 2008 01:41:30 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:bdd50a97-aa69-4645-a33f-3b9e0fc71de9</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
... and think more carefully about pointers, arrays, strings,
etc&lt;/p&gt;

&lt;pre&gt;
xdata unsigned char *Arr[3][20];
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Should be:&lt;/p&gt;

&lt;pre&gt;
xdata unsigned char Arr[3][20];
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
ie, an array of three 20-character strings&lt;br /&gt;
(and don&amp;#39;t forget that the 20 includes the NUL terminator - so that&amp;#39;s
only 19 usable characters)&lt;/p&gt;

&lt;pre&gt;
     Arr[0][20] = &amp;quot;LCD Testing    &amp;quot;;
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
No, you can&amp;#39;t do that!&lt;/p&gt;

&lt;p&gt;
You need to &lt;b&gt;copy&lt;/b&gt; that text into the string&lt;/p&gt;

&lt;pre&gt;
     Arr[1][15] = &amp;quot;Programming of LCD&amp;quot;;
     sprintf(Arr[1][20],&amp;quot;%s%u&amp;quot;,Arr[1][15],Val);
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Again, you can&amp;#39;t do that!&lt;/p&gt;

&lt;p&gt;
you need something like:&lt;/p&gt;

&lt;pre&gt;
     sprintf( Arr[1], &amp;quot;Programming of LCD %u&amp;quot;, Val );
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
and you will need to repeat that each time you want to display a new
value of &amp;#39;Val&amp;#39;...&lt;/p&gt;

&lt;p&gt;
I still don&amp;#39;t think this array of strings is helping you - you are
still going to have to rebuild each string immediately before you
display it - so you might just as well use printf directly!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/141961?ContentTypeID=1</link><pubDate>Tue, 18 Mar 2008 01:40:51 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:611091d1-3db5-499d-b57d-76f36eee2701</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Andy as u said i have changed the 1D array to 2D array but
according the above the result was,&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Wrong! You have added an extra dimension to your array, but are
still playing with pointers, instead of characters. Away with the
star in the declaration.&lt;/p&gt;

&lt;p&gt;
Your code:&lt;/p&gt;

&lt;pre&gt;
     Arr[1][15] = &amp;quot;Programming of LCD&amp;quot;;
     sprintf(Arr[1][20],&amp;quot;%s%u&amp;quot;,Arr[1][15],Val);
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Will manage the first assign, but the line will not mean what you
think it means.&lt;/p&gt;

&lt;p&gt;
The second line - sprintf() will still not print to a text buffer,
but will get an unassigned pointer to use for buffer. And even if Arr
would have been correctly initialized with 15*20 pointers, the
individual indices are 0..14 and 0..19, and you are making use of the
index 20, i.e. the 21th entry of a dimension that has only 20
entries...&lt;/p&gt;

&lt;p&gt;
But for the optimum, replace Val with an array of 15 entries, and
build the string whenever the users presses the up or down button.
Then you don&amp;#39;t need the sprintf() at all.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/138707?ContentTypeID=1</link><pubDate>Tue, 18 Mar 2008 01:04:40 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c3dcf628-73a7-4247-880a-1da7addd90d2</guid><dc:creator>Gowri Shankar</dc:creator><description>&lt;p&gt;&lt;pre&gt;
#include&amp;lt;reg669.h&amp;gt;
#include&amp;lt;stdio.h&amp;gt;
&lt;/pre&gt;
&lt;pre&gt;
xdata unsigned char *Arr[3][20];
xdata unsigned int  Val = 1;
xdata unsigned int  Val1 = 2;
&lt;/pre&gt;
&lt;pre&gt;
&lt;br /&gt;
void main(void)
  {
     S0CON = 0x50;   //SERIAL COM IN MODE 1
     TMOD  = 0x20;   //TIMER 1 IN AUTO RELOAD MODE
     TH1   = 0xFD;   //BAUD RATE IS 19200
     TL1   = 0x00;
     TR1   = 1;   //ENABLE TIMER 1

     Arr[0][20] = &amp;quot;LCD Testing    &amp;quot;;

     Arr[1][15] = &amp;quot;Programming of LCD&amp;quot;;
     sprintf(Arr[1][20],&amp;quot;%s%u&amp;quot;,Arr[1][15],Val);

     Arr[2][15] = &amp;quot;Sprintf testing &amp;quot;;
     sprintf(Arr[2][20],&amp;quot;%s%u&amp;quot;,Arr[2][15],Val1);

     SBUF=&amp;#39; &amp;#39;;
     printf(Arr[0][20]);
     TI_0 = 0;
     Delay(1000);

     SBUF=&amp;#39; &amp;#39;;
     printf(Arr[1][20]);
     TI_0 = 0;
     Delay(1000);

     SBUF=&amp;#39; &amp;#39;;
     printf(Arr[2][20]);
     TI_0 = 0;
     Delay(1000);

     while(1);
  }&lt;br /&gt;
&lt;/pre&gt;

&lt;p&gt;
Andy as u said i have changed the 1D array to 2D array but
according the above the result was,&lt;/p&gt;

&lt;pre&gt;
LCD Testing
Sprtinf testing 2
Sprintf testing 2
&lt;/pre&gt;

&lt;p&gt;
If i disable the sprintf statement and print the ouput for
Arr[2][20] then the output is,&lt;/p&gt;

&lt;pre&gt;
LCD Testing
Programming of LCD 1
Programming of LCD 1
&lt;/pre&gt;

&lt;p&gt;
Even though i disabled sprtinf statement for Arr[2][20].&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/135316?ContentTypeID=1</link><pubDate>Tue, 18 Mar 2008 00:52:12 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:db5ac4e7-5a34-4afb-9f92-9c80067a2495</guid><dc:creator>Gowri Shankar</dc:creator><description>&lt;p&gt;&lt;p&gt;
Yep Andy. U r rite..Out of 20 characters a part of it already
fixed as strings...Only part i need to append along with the
strings.&lt;/p&gt;

&lt;p&gt;
For example,&lt;/p&gt;

&lt;p&gt;
here is the string fixed =&amp;quot;On time = &amp;quot;&lt;br /&gt;
along with that i need to add &amp;quot;60&amp;quot; after equal to symbol as this 60
is represented as integer and kept in another variable.&lt;/p&gt;

&lt;p&gt;
So i used,sprintf statment to append the both then comes the
problem when using array of pointers...&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/124516?ContentTypeID=1</link><pubDate>Tue, 18 Mar 2008 00:44:41 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ccb03cf5-02df-4d8f-846c-013cc93f7c7d</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;But i need to display 15 datas during run time of a program in
the LCD.&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
If they change at run time, you are going to have to rebuild the
string with the new value each time it is requested for display,
aren&amp;#39;t you?&lt;/p&gt;

&lt;p&gt;
You can&amp;#39;t just build the strings once and then just select one for
display, can you?&lt;/p&gt;

&lt;p&gt;
So I can&amp;#39;t see how your approach will actually save anything over
using printf directly?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/113745?ContentTypeID=1</link><pubDate>Tue, 18 Mar 2008 00:40:09 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b1b63145-598c-407c-a58f-dc03ab582b84</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
You will use a lot of unnecessary XDATA space in this approach for
storing the complete string, when only the value part of it actually
changes!&lt;/p&gt;

&lt;p&gt;
Of course, if you have plenty of XDATA space, that may not be a
problem for you.&lt;/p&gt;

&lt;p&gt;
In this case, you need the array of &lt;b&gt;strings&lt;/b&gt; - not just an
array of pointers!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/100389?ContentTypeID=1</link><pubDate>Tue, 18 Mar 2008 00:07:52 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:fae01530-a9fb-46ec-aaae-d07c8526f755</guid><dc:creator>Gowri Shankar</dc:creator><description>&lt;p&gt;&lt;p&gt;
Printing directly is simple and straight forward. But i need to
display 15 datas during run time of a program in the LCD. In order to
view the datas user will use &amp;quot;UP&amp;quot; or &amp;quot;Down&amp;quot; key. So when user uses
that i need to display it accordingly.&lt;/p&gt;

&lt;p&gt;
Thats is y i declared array pointer and the array element i will
use a variable to increment or decrement the value according to the
UP or Down key pressing.&lt;/p&gt;

&lt;p&gt;
Hence based on that the display will change and user can review
the datas.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/88955?ContentTypeID=1</link><pubDate>Tue, 18 Mar 2008 00:02:27 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:7e48ea37-dc57-4fac-b081-32a260805dac</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
Why not just use printf directly:&lt;/p&gt;

&lt;pre&gt;
     SBUF=&amp;#39; &amp;#39;;
     printf( &amp;quot;LCD Testing    &amp;quot; );
     TI_0 = 0;
     Delay(1000);

     SBUF=&amp;#39; &amp;#39;;
     printf( &amp;quot;Programming of LCD %u&amp;quot;, Val );
     TI_0 = 0;
     Delay(1000);

     SBUF=&amp;#39; &amp;#39;;
     printf( &amp;quot;Printf testing %u&amp;quot;, Val1 );
     TI_0 = 0;
     Delay(1000);

&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Re: Sprintf error</title><link>https://community.arm.com/thread/51619?ContentTypeID=1</link><pubDate>Mon, 17 Mar 2008 23:57:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:d729e0a4-cad6-464c-89ee-a590ce921bef</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;pre&gt;
xdata unsigned char *Arr[3];
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Simply creates 3 &lt;b&gt;pointers&lt;/b&gt; - you also need to have something to
store the actual &lt;i&gt;&lt;b&gt;contents&lt;/b&gt;&lt;/i&gt; of your 3 strings; eg&lt;/p&gt;

&lt;pre&gt;
xdata unsigned char Arr[3][MAX_STRING_LENGTH];
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>