problem in c code while(*s)

Status
Not open for further replies.

engineer khan

Member level 3
Joined
Aug 31, 2012
Messages
66
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Visit site
Activity points
1,833
Hello everybody ! please explain the following code to me
whats while(*s) how it works


Code C - [expand]
1
2
3
4
5
void send_to_modem(char *s){
 while(*s)
 uart1_write(*s++);
 uart1_write(0x0d);
}

 

The code writes a zero-terminated string to UART1, adding carrier return in the end. "while(*s)" is a short form of "while((*s) != '\0')", which means loop while char, "s" points to is not equal to zero. Note that "s" is incremented on each iteration, so that s points to the next character in the string.
 
s is a pointer to a location in memory that holds a variable of type char. *s retrieves the variable value that s points to.
As the pointer moves to the next location in memory in the expression *s++ the loop while(*s) { ... } will be executed as long as the value that s points to is not 0.
 
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…