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.
Andy as u said i have changed the 1D array to 2D array but according the above the result was,
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.
Your code:
Arr[1][15] = "Programming of LCD"; sprintf(Arr[1][20],"%s%u",Arr[1][15],Val);
Will manage the first assign, but the line will not mean what you think it means.
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...
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't need the sprintf() at all.