void fn(char* c)
{
if(c == "hi")
cout << "EQUAL";
else
cout << c;
}
int main()
{
char str[1][3];
strcpy(str[0],"hi");
fn(str[0]);
return(0);
}
thanks,
skitzo_smurf
darn strings
could someone tell me why the following source code outputs
NOT EQUAL instead of hi ?
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
That''s because "c" is a pointer to the first char in your string, and not the STRING itself. In order to check if two strings are the same, you can use the strcmp() funcy... it''s used as follows...
Hope this helps!
..-=ViKtOr=-..
void fn(char* c){ if(strcmp(c, "hi") == 0) cout << "EQUAL"; else cout << c;}int main(){ char str[3] = "hi"; fn(str); return(0);}
Hope this helps!
..-=ViKtOr=-..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement