error: assignment to expression with array type

2

I'm trying to compile the code below for a list of exercises from my course course of introduction to programming but the codeblocks throws the error mentioned in the title when I try to compile the code.

#include<stdio.h>
int main(){
    float peso,altura,imc;
    char nome[40],categoria[40];

    printf("Qual o nome da pessoa? ");
    fflush(stdin);
    gets(nome);

    printf("\nQual a altura dessa pessoa em metros? ");
    scanf("%f",&altura);

    printf("\nQual o peso dessa pessoa em kilos? ");
    scanf("%f",&peso);

    imc=peso/(altura*altura);

    if(imc<18.5){
        categoria='abaixo do peso';
    }

    if((18.5<=imc)&&(imc<25)){
        categoria='peso normal';
    }

    if((25<=imc)&&(imc<30)){
        categoria='acima do peso';
    }

    if(imc>=30){
        categoria='obesidade';
    }

    printf("\n%s esta com indice de massa corporal %f(%s)", nome, imc, categoria);

    return 0;
}
    
asked by anonymous 08.05.2018 / 15:01

1 answer

2

The problem is here:

categoria='abaixo do peso';

To begin with, you must be double quotation marks " instead of single quotation marks ' , and it can not be assigned directly as you are doing.

To copy the text to the array of char you have, you must use the function strcpy :

strcpy(categoria, "abaixo do peso");
//       ^--- destino      ^--- texto a copiar

Since this function is part of the string library, it is important to import it at the top:

#include <string.h>

You should apply strcpy to the remaining parts of the code that had the same problem.

As a recommendation for the future, avoid using the gets function at all costs because it is not secure.

    
08.05.2018 / 15:11