Hello everybody, Wish you all a very happy new year.
I am facing a problem, I know basically it is a C language problem but I am still not able to catch up the language yet.
I have reversed the data pins of LCD. Now while programming I can reverse the data byte by byte, but the speed of the LCD is quite low.
void LCD_sendstring(unsigned char *strLCD){ // e.g. LCD_sendstring("LCD Tutorial"); while(*strLCD) //till string ends LCD_senddata(*strLCD++); //send characters one by one } void LCD_senddata(unsigned char var){ // e.g. LCD_senddata('A'); LCD_data = reverse(var); LCD_rs = 1; //Selected data register LCD_rw = 0; //We are writing LCD_en = 1; //Enable H-> LCD_en = 0; LCD_busy1(); //Wait for LCD to process the command }
As the above code reverses each character before sending to LCD, the characters appears slowly on LCD. So I am trying to reverse the whole string before sending to the LCD (LCD_senddata). I have tried the following codes but its not working.
void LCD_sendstring(unsigned char *strLCD){ // e.g. LCD_sendstring("LCD Tutorial"); reverse_string(strLCD); *strLCD = reverse_string1(strLCD); while(*strLCD) //till string ends LCD_senddata(*strLCD++); //send characters one by one } void LCD_senddata(unsigned char var){ // e.g. LCD_senddata('A'); LCD_data = var; LCD_rs = 1; //Selected data register LCD_rw = 0; //We are writing LCD_en = 1; //Enable H-> LCD_en = 0; LCD_busy1(); //Wait for LCD to process the command } void reverse_string(char *str){ //junk result while(*str){ // *str=reverse3(*str); *str='B'; str++; } str = &str; } char *reverse_string1(char *str){ //blank result int len = strlen(str); char *newstr = malloc(len+1); while(*str){ //till string ends // *newstr++ = reverse(*str++); // *newstr++ = *str++; *newstr++ = 'B'; } *newstr = '\0'; return newstr; }
In reverse_string I get junk result whereas in reverse_string1 I am getting blank display. Please Help!!!
Note that your new variable is a pointer to characters.
So you have allocated space to store one memory addrss. You haven't allocated space to store any characters. So you can't use your pointer as destination for any strcpy().
Maybe time to pick up a book about the C language, and read up on the similarity (and differences) between pointers and arrays - and that you need memory for the full array content if you want to copy strings. When you manipulate the characters of a string, it's important that you understand exactly where the actual characters are stored.
Thanks, it is almost clear to me now.
One more question pleeeease. We increment pointers (like *strLCD++) in while loop. If again we have to use it in another loop how should we reset the pointer to start of the string?
Just store a copy of the original pointer value:
unsigned char *start = strLCD; while (*strLCD) { funny(*strLCD++); } strLCD = start; while (*strLCD) { funnier(*strLCD++); }
Thanks!!!