how to check a string in if [duplicate]

-1

I have the code: What reason he is not giving positive to if?

{
    char placa[9] = "G" "A" "E" "-" "0" "2" "4" "4";
    char cor[15] = "branco";
    int ano = 2001;

    printf("A placa: %s\n", placa);
    printf("A cor: %s\n", cor);
    printf("O ano: %d\n", ano);

    if(cor == "branco")
    {
        printf("voce tem um carro branco \n");
    }
    else 
    { 
        printf("voce nao tem um carro branco \n");
    }

    system("PAUSE");

    return 0;
}
    
asked by anonymous 14.08.2018 / 13:05

1 answer

0

color is a pointer to an array of chars then when you do the comparison

cor == "branco" 

You are comparing the value of pointers and not words, to compare the words you can use the strcmp function. a> (there are several ways to do this comparison, such as creating your own function or using one from a library you are using).

    
14.08.2018 / 13:14