c - Check the value of character variable -


i'm writing code arduino , i'm not sure if i'm checking value of character variable correctly. can please tell me if correct:

const char* front = "front"; const char* = "back";  eyeballs(front); eyeballs(back);  void eyeballs(const char* frontorback){   if (frontorback == "front") {     digitalwrite(fronteyes, low);}//end if      else if (frontorback == "back") {     digitalwrite(backeyes, low);}//end else*/ }   

you need use strcmp() compare c-strings. pointer comparison.

if ( strcmp(frontorback, "front") == 0 ) {    digitalwrite(fronteyes, low);}//end if      else if ( strcmp(frontorback, "back") == 0 ) {     digitalwrite(backeyes, low);}//end else*/ }   

in comparison,

if (frontorback == "front") { 

the pointer value frontorback compared address of string literal "front" (in expression, string literal gets converted pointer first element).


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -