need explanation in lcd interface with 8051 coding

Status
Not open for further replies.

rangerskm

Full Member level 4
Joined
Jan 23, 2013
Messages
199
Helped
0
Reputation
2
Reaction score
0
Trophy points
1,296
Visit site
Activity points
2,663
Code:
void display_lcd(unsigned char *s)

{

while(*s)

write_lcd(*s++);

}
can any one explain the above coding??
 
Last edited by a moderator:

Typically in the C language character strings are terminated with a NULL (\0) which has a byte value of 0x00.

The pointer to type char s, which is initialized to the first character of the character string, is first dereferenced using the * operator, then checked by the while loop to ensure it is not the NULL character or value 0x00.

If not, the pointer currently points to a character member of the string which is then passed to the write_lcd() to be output on the LCD.

Also notice immediately after passing the current character to the write_lcd(), the pointer s is increment (*s++), essentially moving the pointer s to the next character.

The execution flow returns to the while loop and repeats the process until the current character is the terminating NULL character or the value 0x00.


BigDog
 

problem in lcd interfacing with 8051 coding

Code:
lcd_dataa(unsigned char *disp)    // function to send string to LCD
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);

}
}

can any one explain the above coding in lcd interfacing and compare with the below coding

Code:
void display_lcd(unsigned char *s)

{

while(*s)

write_lcd(*s++);

}
 

The pointer variable s actually contains an address, which contains a character value.

Therefore, when s is dereferenced using the * operator the character value is return instead of the address value.


Pointers can be a challenging topic for many people to understand, review the following tutorials and then post any additional questions:

A TUTORIAL ON POINTERS AND ARRAYS IN C

C - Pointers

Lesson 6: Pointers in C


BigDog

- - - Updated - - -

Code:
lcd_dataa(unsigned char *disp)    // function to send string to LCD
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);

}
}

The lcd_dataa() routine performs a similar function as the previous display_lcd(), the main difference is the character string is incremental accessed using array index methods rather than the pointer methods.

The pointer of type character disp when combined with the square index brackets [], essentially provides an array of type character by which each element of the array can be accessed by incrementing or decrementing the index.

Each time index x is incremented in the for loop, the contents of the array element disp[x] is checked for a NULL or value 0x00, just as was done using the pointer methods.

If the current element of the character array disp is not NULL, it is passed to the lcd_data() routine.

Essentially, the lcd_dataa() incrementally steps through each element of the character array disp and passes these characters to the lcd_data() routine, until the NULL is encounter which terminates the routine.


Initially, before the char pointer is incremented:

*s == s[0] o r *disp == disp[0]


BigDog
 
what will happen to the above code if we use variable instead of pointer variable ??
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…