I can not compare char []

0

I'm running a server that I've done in C and it's working normally, with one exception: I can not create a condition for received bytes. The Write and Recv functions work normally but I'm falling into a silly error in which the Else clause is always enabled. I set up my client for ASCII but I did not succeed.

Image:

    
asked by anonymous 23.03.2017 / 00:25

1 answer

1

You are using the == logical operator to compare responseLogin with "ADMIN" . What is happening, in fact, is the comparison of the memory addresses of the arrays responseLogin and "ADMIN" . That way, since the pointers are distinct, the condition is always false.

To achieve what you really want, that is, to compare the content of the character arrays, you must use a function like strcmp . Note:

char *responseLogin = "ADMIN"; 

if (responseLogin == "ADMIN") // condição falsa, os ponteiros comparados são diferentes
{
    // ...
}

if (strcmp(responseLogin, "ADMIN") == 0) // condição verdadeira, os conteúdos são iguais
{
    // ...
}
    
23.03.2017 / 01:00