language c is giving me a small error and I can not correct

Asked

Viewed 6,852 times

-1

I have to do a job and it’s giving me a mistake that I’m not able to fix, the mistake says:

assignment to Expression with array type

The error is in the void inicializarcategoria()I really 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;
}

3 answers

0

Cannot assign a string to a char array in C.

try to change that:

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

That’s why:

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

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

0

The error is on the line

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

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

cat[i].nome = "nome_da_categoria";
  • Another thing, to compare strings, I recommend using strcpy, from the string library. h

  • 1

    it seems to me that it would be strcmp

  • 1

    Sorry, I was wrong!

0

Your entire code is in error:

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

  • As his struct uses a value vector, char nome[60], it is necessary to use the function strcpy or sprintf to pass the value inside the vector.

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

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

  • In function criar_categoria is wrong in the comparison of String within the for cat[i].nome != nome_da_categoria. The comparison should be made by the function strcmp. 1 if different and 0 equal.

    && (!strcmp(cat[i].nome, nome_da_categoria)) &&

  • Us ifs just below the same error of comparing and assigning the value of its String.

Note: When writing an error on screen, use fprintf, or if it is the error of a function such as malloc use perror.

fprintf(stderr, "Error de sistema\n");
perror("My Error");

Browser other questions tagged

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