I can’t compare char[]

Asked

Viewed 241 times

0

I am running a server that I did in C and is working normally, with only one exception: I cannot create a condition for the received bytes. The Write and Recv function work normally but I am falling into a silly error in which the Else clause is always activated. I set up my client for ASCII but did not succeed.

Imagery:

enter image description here

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 matrices responseLogin and "ADMIN". Thus, as the pointers are distinct, the condition is always false.

To achieve what you really want, namely to compare the contents of the character matrices, you must use a function such as the strcmp. Observe:

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
{
    // ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.