Hello
I have function that looks like this myfoo(char* Name)
Now i want to compare this name to another one .
But the another name is a pointer .
this my code :
}
I need to compare if p is same as H_Name.
Any ideas .
Mine is do it with for on each element but when i use sizeof it gives me size of char and not real size of the name.
Thx.
(Does class Tribe use char*s to store its strings internally?)
I prefer Peter87's strcmp approach to a home grown loop.
(Note that strcmp does not protect itself against null string, so if either param is null, it will die. If nulls are valid, I provide a "safe_strcmp" function which checks for nulls first.)
Also, it looks like the Namechar param of RemoveSurvavio could be a const char* ??
Andy
PS When it comes to home grown, it can be simplified if you initialize same to true and rejig the terminaton condition rejig the terminaton condition and then check that the strings terminate at the same point.
1 2 3 4
bool same = true; // assume they are equal
for(size_t i=0; p[i] != '\0'; ++i) {
if (H_Name[i] != p[i]) { same = false; break; }
}
or even
1 2 3 4 5 6 7
bool same = true; // assume they are equal
for(size_t i=0; p[i] != '\0'; ++i) {
if (H_Name[i] != p[i]) {
same = false; // nope, they're not
break;
}
}
@andywestken, that loop wont work. If p[i] is \0 and H_Name[i] is eg letter Z then your loop is going to say they are equal and exit.
There is a good reason why I used 2 ifs instead of the condition in the for.