This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Re: Sprintf error

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<reg669.h>
#include<stdio.h>

xdata unsigned char *Arr[3];
xdata unsigned int  Val = 1;
xdata unsigned int  Val1 = 2;

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] = "LCD Testing "; Arr[1] = "Programming of LCD"; sprintf(Arr[1],"%s%u",Arr[1],Val); Arr[2] = "Sprintf testing "; sprintf(Arr[2],"%s%u",Arr[2],Val1); SBUF=' '; printf(Arr[0]); TI_0 = 0; Delay(1000); SBUF=' '; printf(Arr[1]); TI_0 = 0; Delay(1000); SBUF=' '; printf(Arr[2]); TI_0 = 0; Delay(1000); while(1); }

The 1st printf works fine whereas for the 2nd and 3rd printf statments the integer value doesnt come alont with string. I dont know the reason....

I am clueless regarding the mistake i have done.

Parents
  • ... and think more carefully about pointers, arrays, strings, etc

    xdata unsigned char *Arr[3][20];
    


    Should be:

    xdata unsigned char Arr[3][20];
    


    ie, an array of three 20-character strings
    (and don't forget that the 20 includes the NUL terminator - so that's only 19 usable characters)

         Arr[0][20] = "LCD Testing    ";
    


    No, you can't do that!

    You need to copy that text into the string

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


    Again, you can't do that!

    you need something like:

         sprintf( Arr[1], "Programming of LCD %u", Val );
    


    and you will need to repeat that each time you want to display a new value of 'Val'...

    I still don'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!

Reply
  • ... and think more carefully about pointers, arrays, strings, etc

    xdata unsigned char *Arr[3][20];
    


    Should be:

    xdata unsigned char Arr[3][20];
    


    ie, an array of three 20-character strings
    (and don't forget that the 20 includes the NUL terminator - so that's only 19 usable characters)

         Arr[0][20] = "LCD Testing    ";
    


    No, you can't do that!

    You need to copy that text into the string

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


    Again, you can't do that!

    you need something like:

         sprintf( Arr[1], "Programming of LCD %u", Val );
    


    and you will need to repeat that each time you want to display a new value of 'Val'...

    I still don'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!

Children
  • Can't you just use something like:

    void display_a_value( unsigned char value_id )
    {
       switch( value_id )
       {
          case 0: printf( "this value is: %u" this_value );
                  break;
    
          case 1: printf( "that value is: %u" that_value );
                  break;
    
          case 2: printf( "the other value is: %u" the_other_value );
                  break;
    
          etc...
       }
    }
    

    Of course, you'd need some meaningful identifiers, and there's room for some optimisation - but that's the basic idea.

    Always start with the basics; optimise later!