pointer to string 'comparison'

I need to understand what's going on in the following question. I was given a char * array(c2). I know that this is the 'C' way of doing things and in C++ we should use string containers and vectors. Still i want to understand it this way.

I want to find out the number of times a certain string e.g. "sunday" exists in the array. When i build and run the code, it gives me the correct answer('8' in this case).

1)Why/How does the following line of code works?

 
if(c2[i] == "sunday") 


I am comparing an address(since c2 is an array of pointers) to a string. Is it working because "sunday" is a char array and hence an address at the end of the day? (Offcourse the addresses on both sides of the equation should be same as pointer is caring the address of the string itself)

Note: i also think that the array should be 'const char *' instead of just 'char *' but this is how it was given to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  int main(void)
{
    int c1[16] = {-3,2,0,7,4,3,2,1,100,16,1,0,-13,6,2,3};
    const char* c2[16] = {"a","b","sunday","sunday","sunday", "sunday","sunday","sunday" ,"b","sunday","ahgijfdo","","sunday","","abc","monday"};

    unsigned int counter = 0;
    size_t array_elements = 0;
    array_elements  = sizeof(c2) / sizeof(c2[0]);

    string temp = c2[2];
    cout<<"Number of array elements are: "<< array_elements <<endl;


    for(size_t i = 0; i< array_elements; ++i)
    {
        if(c2[i] == "sunday")
        {
            ++counter;
        }
    }
    cout<<"Counter value is : "<<counter<<endl;

    return 0;
}
One important note: "sunday" is not a string. It is of type const char*. It is *not* an std::string.

This code probably won't work. (But it might). The compiler might decide that both instances of "sunday" in the program should be stored at the same address since they are the same constant, but it might also decide to use two different instances of that literal in memory.
why is line 3 there?

ok so first of all there are multiple things happening here:

c2 is not a const char* its an array of const char* which are pointers to char arrays.
"sunday" is a char array so c2 is actually in essence a const char c2[][]
but instead of storing the char s directly they're being stored somewhere else so you save the pointer(*) to them in the array.

so youre comparing the data of elements in an array like as though you were to count the number of 3's in c1.

all operators in c++ are functions. so there is a function that looks like this that the compiler makes to compare c-strings:

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


so you can totally compare the elements to another thing of the same type (const char *)
I hope this clears things up? pointers are the hardest part of c and c++
@firedraco As you said that the code might or might not work. So what should be a safe and standard way of doing this sort of comparison with the given input arrays?
As firedraco has said, if it works, it is pure luck. The == operator will only compare addresses, not the actual contents of the C-style strings, and you cannot rely on the literal "sunday" on line 4 necessarily being located at the same address as the literal "sunday" mentioned on 16.

To compare C-style strings, use strcmp() instead of operator==().

@nubforce: Your description of operator==() or character arrays is not correct. They are treated as pointers and their addresses are compared. You code looks more like an implementation of strcmp(), which actually compares the contents.
@Zhuge Got it now. Thanks
Topic archived. No new replies allowed.