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

Submitted by saj on

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

 

 

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

  Joined January 21, 2019      42
Monday at 12:12 PM

"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++;

}

 

  Joined January 21, 2019      42
Monday at 12:12 PM

to tell you more clearly, just post your code.

  Joined January 21, 2019      42
Monday at 12:12 PM

 
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.

  Joined January 21, 2019      42
Monday at 12:12 PM

If '&&' is logical-AND and '*s' is "not-s" then the logic 
is "never" and nothing happens. 

  Joined May 28, 2019      4
Tuesday at 05:40 PM

No you are wrong...

'*s' is not "not-s" . *s indicates a pointer variable. not s will be represent by "!s"

  Joined August 14, 2018      44
Tuesday at 03:25 PM