language and you give me a small error and I can not correct

-1

I have to do a job and this gives me a mistake that I can not fix, the error says:

  

assignment to expression with array type

The error is in void inicializarcategoria() even need help

#include <stdio.h>
#include <stdlib.h>

#define TAMANHO 60


typedef struct categoria{
char nome[60];
}CATEGORIA;

typedef struct produto {
    char nome[60];
    int kcal;
    int lactose;
    CATEGORIA categoria;
}PRODUTO;

    typedef struct compra{
        char nome_compra[100];
    }COMPRA;

CATEGORIA cat[TAMANHO];
PRODUTO prod[TAMANHO];

void inicializarcategoria(){
    int i; cat[i].nome = nome_da_categoria;
    for(i = 0;i < TAMANHO ;i++);
        cat[i].nome = NULL;
}


void criar_categoria(char* nome_da_categoria){
    int i;
    for(i = 0; cat[i].nome != NULL && i < TAMANHO && cat[i].nome != nome_da_categoria; i++);
    if(i == TAMANHO)
        printf("erro tamanho do array excedido\n");
    if(cat[i].nome == nome_da_categoria)
            printf("ja existe categoria\n");
     if(i < TAMANHO && cat[i].nome != nome_da_categoria)
        cat[i].nome = nome_da_categoria;
}


int main(){
inicializarcategoria();
inicializarproduto();
menu();
menulistacompra();

return 0;
}
    
asked by anonymous 06.01.2017 / 19:50

3 answers

0

You can not assign a string to a char array in C.

try changing this:

... 
cat[i].nome = nome_da_categoria;
...

So:

  sprintf(cat[i].nome, "%s", nome_da_categoria);

I also noticed that you are declaring int i and then using it as an index. So it will surely pick up memory junk and point to a random index probably unallocated. I recommend taking a look at this.

    
06.01.2017 / 20:48
0

The error is on the line

int i; cat[i].nome = nome_da_categoria;

The program does not know what category_name is, because this has not been defined, if you want to add the cat [i] .name, category_name, do so

cat[i].nome = "nome_da_categoria";
    
06.01.2017 / 20:50
0

Your entire code is in error:

  • int i; cat[i].nome = nome_da_categoria; . The variable i has garbage, so its value can be -36, 986, etc. This causes memory errors in your vector. Always start a value for i .

  • li>

  • struct is not instantiated within its function, so it will always give the error of the nonexistent variable. Get it by parameter.

    void initialize category (const char category_name) {     int i = 0;     strcpy (cat [i] .name, category_name); }

  • In char nome[60] function has error in comparing String within strcpy sprintf . The comparison must be made by the nome_da_categoria function. 1 if it is different and 0 if it is the same.

    criar_categoria

  • )
  

Note: When writing an error on screen, use for , or if it is the error of some function like cat[i].nome != nome_da_categoria use strcmp .

fprintf(stderr, "Error de sistema\n");
perror("My Error");
    
06.01.2017 / 21:24