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