I just created this function to check the equality between two strings by subtracting their Integer values
int EQ(const char* CString1, const char* CString2)
{
return CString1 - CString2 == 0;
}
However, I'm not sure if this is a safe way to check for char*
Strings
It won't work with arrays cause they've different integers so I created other function for that purpose
int EQA(const char* CString1, const char* CString2){
if(CString1 == NULL || CString2 == NULL)
return 0;
int i = strlen(CString1);
if(i != strlen(CString2))
return 0;
while(i>=0){
if(CString1[i] != CString2[i])
return 0;
--i;
}
return 1;
}
I didn't try to optimize my second function too much
But when checking on char*
, why would I use EQA()
or strcmp()
over EQ()
?
the first one EQ() is much faster than any other standard function I used before
So I really liked using it
I tested it on other machines and it worked but the Integers values of char*
differ, also
noticed that these integers depend on which variable is declared first in the code but it's the same integer if they have the same string which makes this function works perfectly, So does it depends on the compiler?
and why different integers?, Is it still safe?
EDIT: Some answers focused on debugging my 2nd example rather than answer my question, So I edited it.