Size of char arry

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 :


bool Tribe::RemoveSurvavior(char *H_Name)
{
const char *p;



p=SurpointArr[i]->GetSurvivor_Name();




}
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.

If the arrays are null terminated (if they are C strings) you can use std::strcmp.
http://en.cppreference.com/w/cpp/string/byte/strcmp
I wanted to tell you how to compare 2 strings but writing the code is shorter.
1
2
3
4
5
bool same;
for(size_t i=0;;i++) {
    if (H_Name[i] != p[i]) { same = false; break;}
    if (p[i] == '\0') { same = true; break;}
}
(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;
    }
}


or even

1
2
3
4
5
6
7
bool are_equal(const char* lhs, const char* rhs) {
    for(size_t i=0; lhs[i] != '\0'; ++i) {
        if (lhs[i] != rhs[i])
            return false;
    }
    return true;
}


1
2
3
4
5
6
bool result = true;
size_t i=0;
for(i=0; p[i] != '\0'; ++i) {
    if (q[i] != p[i]) {result = false; break;}
}
result = result && (q[i] == '\0');


or

1
2
3
4
5
6
7
8
9
bool result = true;
size_t i=0;
for(i=0; p[i] != '\0'; ++i) {
    if (q[i] != p[i]) {
        result = false;
        break;
    }
}
result = result && (q[i] == '\0');


or even

1
2
3
4
5
6
7
8
bool are_equal(const char* lhs, const char* rhs) {
    size_t i=0;
    for(i=0; lhs[i] != '\0'; ++i) {
        if (rhs[i] != lhs[i])
            return false;
    }
    return ('\0' == rhs[i]);
}
Last edited on
@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.
This seems like a lousy candidate for a for loop.

1
2
3
4
5
6
7
bool are_equal(const char* a, const char* b)
{
    while ( *a == *b && *a)
        ++a, ++b ;

    return *a == *b ;
}
@zoran404

Thanks for pointing out my mistake,

How about

1
2
3
4
5
6
7
8
bool are_equal(const char* lhs, const char* rhs) {
    size_t i=0;
    for(i=0; lhs[i] != '\0'; ++i) {
        if (rhs[i] != lhs[i])
            return false;
    }
    return ('\0' == rhs[i]);
}


Andy
Topic archived. No new replies allowed.