what is this statement //while (s && *s)send_char (*s++);

saj's picture
saj

Offline

Joined: Sep 28, 2018

Replies: 12
what is this statement //while (s && *s)send_char (*s++);
March 1, 2019 - 6:09pm

Hi,

Below part code for 16x2 LCD,  

------------------

void send_string(const char *s)         //send a string to display in the lcd
{          
     while (s && *s)send_char (*s++);
}

---------------------

void send_char(unsigned char data)      //send lcd character

..loop

}

----------------------

I trying to understand how the code //while (s && *s)send_char (*s++); is executed ?? if the argument is passed like "ABCD" ????

I only known the statement in c that //while(), but what is the meaning of that while()with the passing argument by calling function ..ie,  //while (s && *s)send_char (*s++);

could you please clear anybody. and can you share a link whare such statement is exist please.

tnx

 

 

Abhiemanyu Pandit's picture

Offline

Joined: Jan 21, 2019

Replies: 13
what is this statement //while (s && *s)send_char (*s++);
March 4, 2019 - 1:44pm

it would be easy to understand and explain it to if you can send the whole code with declaration of 's'

Abhiemanyu Pandit's picture

Offline

Joined: Jan 21, 2019

Replies: 13
what is this statement //while (s && *s)send_char (*s++);
March 4, 2019 - 3:14pm

"while (s && *s)send_char (*s++);"

This statement simply says that send characters until you find identifier to terminate, to form a string.  here the identifier is null pointer, it means when all characters are displayed then it stops execution until it gets next character. it is the another example of character declaration similar to

char p[4] = "four"; 

or char *s = "four";

above both declarations will work same except that s++ is valid but p++ is not valid.

also in,

char p[4] = "four"; 

The "four" is stored in stack section of memory.

where as in,

char *s = "four"; 

The 's'  is stored in stack but "four" is stored in code section of memory.

 

Also,

"while (s && *s)send_char (*s++);"

and 

"while (s && *s){

send_char (*s++);"

both statement are valid in C.

If there is one statement after condition then you can write a function just ahead of condition. 

e.g:

if(a=b) a++;

or if (a=b){

a++;

}

 

Abhiemanyu Pandit's picture

Offline

Joined: Jan 21, 2019

Replies: 13
to tell you more clearly,
March 4, 2019 - 3:17pm

to tell you more clearly, just post your code.

Abhiemanyu Pandit's picture

Offline

Joined: Jan 21, 2019

Replies: 13
what is this statement //while (s && *s)send_char (*s++);
March 4, 2019 - 3:29pm
 
void send_string(const char *s)         //send a string to display in the lcd
{          
     while (s && *s)send_char (*s++);
}
 
and
 
void sendString(char *str)    // in your case @aj its only 's'
{
unsigned char s=0;     //declare a variable which u have not declared @saj
 
while (str[s]!=0) // string till null 
{
 
s++;   // go to next character
}
}
 
above both code have SAME MEANING.