Help: Valuing a char vector of a struct within an if

Asked

Viewed 375 times

1

I’m trying to define the value of a char vector of a struct within an if, but without success...

The struct itself:

    struct ficha {
        char nome[31], classificacao[31], telefone[21];
        float altura, peso, imc;
    }paciente1, paciente2, aux;

Follow the attempts to:

Tenttiva 1:

    if (paciente1.imc<18.5) {
        paciente1.classificacao = {"Abaixo do peso", 31};
    } else if ...

Attempt 2:

    if (paciente1.imc<18.5) {
        paciente1.classificacao = "Abaixo do peso";
    } else if ...

Attempt 3:

    if (paciente1.imc<18.5) {
        paciente1.classificacao = ("Abaixo do peso", [31]);
    } else if ...

Thanks in advance for your help !

1 answer

1


If I am not mistaken, it is not possible to set the contents of an array directly like this, you have to set the contents of its positions. The function strcpy can help you:

if (paciente1.imc<18.5) {
    strcpy(paciente1.classificacao, "Abaixo do peso");
} else if ...
  • Thanks @Pedro Lorentz, solved the problem !!

Browser other questions tagged

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