String comparision

I am comparing these c strings and it prints out not equal. Why does it do that?
Aren't these the same strings just initialized differently?

#include <iostream>
using namespace std;

int main()
{
char str1[ 5 ];
str1[ 0 ] = 't';
str1[ 1 ] = 'e';
str1[ 2 ] = 's';
str1[ 3 ] = 't';
str1[ 4 ] = '\0';

char str2[] = "test";

// When using this if statment it prints not equal
if( str1 == str2 )
cout << "strings are equal";
else
cout << "not equal";

system( "pause" );
return 0;
}

Thanks for the help.
dude you can only test individual elements. E.G place[1] with place2[1].

if you are trying to compare a string of text, you'll need to use string objects (the string library).
Actually. You're using cstrings here. In order to compare them correctly you need strcmp() from the cstring library.
1
2
3
4
#include <cstring>

if ( strcmp( str1, str2 ) == 0 )
{ ... }
Also take a look at the strcmp function in <cstring>
Thank you.
Topic archived. No new replies allowed.