Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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
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.
 
Read about strings and pointers in C programming books.
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top