Wednesday, 18 September 2013

Compare Two Character Arrays in C

Compare Two Character Arrays in C

I have a string struct.
struct string
{
char *c;
int length;
int maxLength;
}
I want to check if two strings are equal.
So I want to run a for loop.
for(int i = 0; i < length; i++)
if(s1[i] != s2[i]) // This code is more C# than C.
s1 and s2 are both string structs.
How do I do this if(s1[i] != s2[i]) ?
EDIT: I just did this, is it over kill?
for(i = 0; i < length; i++)
if((*s1).c[i] != (*s2).c[i])
{
printf("Failed");
return 0;
}

No comments:

Post a Comment