Need help passing two char arrays and comparing them.

Here's my code. If I enter "stuff" into the console it still returns 'B'. Am I doing something wrong?

char searchit(char[], char[]);

int main()
{
char stuff[6] = "stuff";
char search[6];
char answer;

cout << "Enter Search: ";
cin >> search;

answer = searchit(stuff, search);

cout << answer << endl;

return 0;
}

char searchit(char what[], char ever[])
{
if(what == ever)
{
return 'A';
}
else
return 'B';
}
if(what == ever)

This compares a char pointer to another char pointer.

A char pointer is a single value, usually 4 (or 8) bytes. The value it holds is the memory address is points to. So this
if(what == ever)
is testing if the two char pointers both point to the same memory. They do not both point to the same memory.

If you want to test to see if the memory these pointers point to contains identical data (which is probably what you're trying to do) you'll have to dereference the pointers and check the values.

Alternatively, we have a function to do this for us because it's a pain to write it: strcmp.
That worked thanks.
Topic archived. No new replies allowed.