How to make simple string compare in C (Arduino)? -


i new c , i'm attempting writte simple code arduino (based on wiring language) this:

void loop()  {     distance(cm);   delay(200);     }  void distance(char[3] unit)  {   if (unit[] == "cm")      serial.println("cm"); } 

could please advise me how writte correctly? in advance!

there several ways.

the "basic" 1 using strcmp function:

void distance(char* unit)  {     if (strcmp(unit, "cm")  == 0)          serial.println("cm"); } 

note function returns 0 if strings equal.

if have fixed-length strings maybe testing every single character faster , less resources-consuming:

void distance(char* unit)  {     if ((unit[0] == 'c') && (unit[1] == 'm') && (unit[2] == '\0'))          serial.println("cm"); } 

you can other things (such iterating through arrays if strings can have different length).

bye


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -