Hi guys. I have made a function which compares 2 arrays and tells if they are equal. This is a small portion of a project I am working on. I have written the following code. But it isn't working. On my serial window just Hi is being displayed. Any ideas?
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Hi");
byte pass1[18]="sim123";
byte pass2[18]="sim456";
boolean check ;
check=array_cmp(pass2,pass1,16);
if (check==true)
{
Serial.print("It matches");
}
else
{
Serial.print("No luck");
}
while(1);
}
int array_cmp(byte a[], byte b[], int i)
{
for(int n=0; n<i; i++)
{
if (a[n]!=b[n]){
return false;
}
}
return true;
}
I am not using strcmp because it inputs char type data and I am using byte. Also, I think memcmp compares length rather than element by element checking
Most of the simple routines that are needed for all C code, were problems faced by the designers in 1967. That is why they created the standard library. It behooves you to know it, or at least know to look there when it looks like something that might be a common task.