-1
I have a problem with my C code. My goal is to register categories in my first struct, which occurs without problems. Then, I want to register some subcategories for each category, which causes the following problem in the code:
main.c|45|error: ‘(cate + (sizetype)((long unsigned int)i * 24))->sub’ is a pointer; did you mean to use ‘->’?|
I think I’m wrong about something with the pointer.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
struct categoria_st{
char * nome_categ;
int num_subcateg;
struct subcategorias_st * sub;
};
struct subcategorias_st{
char *nome_subcateg;
};
void le_string(char * buffer, int tam);
int main(){
struct categoria_st * cate;
struct subcategorias_st * subcat;
char aux[5000];
int qtd_cat,i,k,tam,index=0 ,index2 = 0;
printf("Quantidade de categorias que deseja cadastrar:\n");
scanf("%d", &qtd_cat);
cate = malloc(qtd_cat * sizeof(struct categoria_st));
for(i=0; i < qtd_cat; i++){
printf("Digite o nome da categoria:\n");
le_string(aux, 5000);
tam = strlen(aux)+1;
cate[i].nome_categ = malloc(tam * sizeof(char));
strcpy(cate[i].nome_categ, aux);
printf("Digite o numero de subcateg:\n");
scanf("%d", cate[i].num_subcateg);
cate[i].sub = malloc(cate[i].num_subcateg * sizeof(cate->sub));
this part I believe is not correct.
for(k=0;k<cate[i].num_subcateg; k++){
printf("Digite o nome da subcategoria:\n");
le_string(aux, 5000);
tam = strlen(aux)+1;
cate[i].sub.nome_subcateg = malloc(tam * sizeof(char));
strcpy(cate[i].sub[k].nome_subcateg, aux);
}
};
while(index != 99){
printf("Qual nome de categoria deseja vizualisar:\nEscolha entre 1 e %d:\n\n",qtd_cat);
scanf("%d", &index);
printf("Qual nome de subcategoria deseja vizualisar:\nEscolha entre 1 e %d:\n\n",cate[qtd_cat-1].num_subcateg);
scanf("%d", &index2);
printf("Nome da categoria eh: %s\nNome da subcategoria eh:%s\n", cate[index-1].nome_categ,cate[index-1].sub[index2-1].nome_subcateg);
}
return 0;
};
void le_string(char * buffer, int tam){
do {
fgets(buffer, tam, stdin);
} while( strcmp("\n", buffer) == 0);
}
I received a negative point by the question. You can help me improve the way I asked?
– Arduin
I believe the problem is the title: it leads to understand that the question is about how to perform a task, while the question is related to the solution of a compilation error.
– thiagofleal