error: assignement to Expression with array type

Asked

Viewed 519 times

2

I am trying to compile the code below for a list of exercises of my course introduction to programming but the codeblocks presents the error quoted 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;
}
  • I don’t know if it solves the problem, but as a study, it gives a read at that link

1 answer

2

The problem is here:

categoria='abaixo do peso';

For starters it has to be double quotes " instead of single quotes ', and also cannot be assigned directly as you are doing.

To copy text to the array of char which has, 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>

Must apply the strcpy other parts of the code that had the same problem.


As a recommendation for the future, avoid at all costs using the function gets, because she’s not safe.

Browser other questions tagged

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