The C code here below, create a simple list reading from a file that contain some products each one defined by code product, code category, description, number and cost. Nextly, it copy each product in a list of list (or list of category) such that each main node contain the code category and some child nodes that are the products that belong to the category. The problem is in copiaMagazzino() function because I get this warning: "passing argument 1 of 'inserimentoProd' from incompatible pointer type". Is there a way to avoid that without change the structure of the program?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define C 8
#define D 64
typedef struct {
char codP[C];
char codC[C];
char descr[D];
int num;
float costo;
} tipoBaseLista;
typedef struct nodoLista {
tipoBaseLista info;
struct nodoLista *next;
}prodotti;
typedef prodotti *listaP;
typedef struct nodoCat{
char codC[C];
struct nodoCat *next;
struct nodoLista *nodoP;
}categoria;
typedef categoria *listaC;
int menu(void);
void init_list(listaP *l);
int carica_lista(char fName[], listaP *l);
void inserimentoProd(listaP *l, tipoBaseLista p);
void visualizzaLista(listaP l);
void visualizzaMagazzino(listaC l);
void cancella_elemento(listaP *l, char codice_prodotto[], char codice_categoria[]);
int copiaMagazzino(listaC *lc, listaP l);
int main() {
char c;
char filename[] = "Magazzino.txt";
listaP listaProd = NULL;
listaC listaCat = NULL;
char cP[C], cC[C];
for(;;) {
//system("clear");
switch(menu()) {
case 1:
init_list(&listaProd);
printf("\nNumero prodotti caricati: %d\n\n", carica_lista(filename, &listaProd));
//printf("\n\nPress a key...");
//scanf("%c", &c);
break;
case 2:
printf("\nInserisci il codice prodotto da cancellare: ");
scanf("%s", cP);
printf("\nInserisci il codice categoria: ");
scanf("%s", cC);
cancella_elemento(&listaProd, cP, cC);
break;
case 3:
printf("\nNumero categorie caricate: %d\n\n", copiaMagazzino(&listaCat, listaProd));
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
visualizzaLista(listaProd);
break;
case 8:
visualizzaMagazzino(listaCat);
break;
case 9:
system("clear");
exit(0);
}
}
}
int menu(void) {
int v;
printf("Premi 1 per caricare i dati da file di testo\n");
printf("Premi 2 per cancellare un prodotto dalla lista prodotti\n");
printf("Premi 3 per copiare i dati della lista prodotti nel magazzino\n");
printf("Premi 4 per cancellare un prodotto dal magazzino\n");
printf("Premi 5 per stampare i dati del magazzino\n");
printf("Premi 6 per calcolare il prezzo medio e la quantita' media dei prodotti di una categoria\n");
printf("Premi 7 per visualizzare lista prodotti\n");
printf("Premi 8 per visualizzare lista magazzino\n");
printf("Premi 9 per uscire\n");
do {
printf("Enter Code:");
scanf("%d", &v);
}
while(v<0 || v>9);
return v;
}
int carica_lista(char fName[], listaP *l) {
tipoBaseLista prodotto;
int n = 0;
char buf[D] = {0};
char scarto[30];
FILE *f;
f = fopen(fName, "r");
if (f == NULL) {
printf("Non e' possibile aprire il file\n");
exit(1);
}
while (!feof(f)) {
fgets(buf, sizeof(buf), f);
sscanf(buf, "%s%s", prodotto.codP, scarto);
//strcpy(prodotto.codP, buf);
fgets(buf, sizeof(buf), f);
sscanf(buf, "%s%s", prodotto.codC, scarto);
//strcpy(prodotto.codC, buf);
fgets(buf, sizeof(buf), f);
strcpy(prodotto.descr, buf);
fgets(buf, sizeof(buf), f);
sscanf(buf, "%d", &prodotto.num);
fgets(buf, sizeof(buf), f);
sscanf(buf, "%f", &prodotto.costo);
inserimentoProd(l, prodotto);
n++;
}
fclose(f);
return n;
}
void init_list(listaP *l) {
listaP aux;
printf("LIBERO LISTA\n");
while(*l) {
aux = *l;
*l = (*l)->next;
free(aux);
}
}
void init_list_magazzino(listaC *l) {
listaC aux;
printf("LIBERO LISTA\n");
while(*l) {
aux = *l;
*l = (*l)->next;
//init_list((*l)->nodoP);
free(aux);
}
}
void inserimentoProd(listaP *l, tipoBaseLista p) {
listaP pCorrente = NULL;
listaP pPrec;
listaP pNodo;
pNodo = malloc(sizeof(prodotti));
pNodo->info = p;
pNodo->next = NULL;
if (*l == NULL) {
//printf("INSERIMENTO PRIMO ELEMENTO\n");
*l = pNodo;
}
else if(strcmp(p.codP, (*l)->info.codP) < 0) {
//printf("INSERIMENTON IN TESTA\n");
pNodo->next = *l;
*l = pNodo;
}
else {
pPrec = *l;
pCorrente = (*l)->next;
while (pCorrente != NULL && strcmp(p.codP, pCorrente->info.codP) > 0) {
pPrec = pCorrente;
pCorrente = pCorrente->next;
}
if(pCorrente == NULL) {
//printf("INSERIMENTON IN CODA\n");
pPrec->next = pNodo;
}
else {
//printf("INSERIMENTON IN MEZZO\n");
pNodo->next = pCorrente;
pPrec->next = pNodo;
}
}
}
void visualizzaLista(listaP l) {
if(l == NULL) {
printf("Lista vuota. Nulla da visualizzare!\n");
}
else {
while(l != NULL) {
printf("\nCodice prodotto: %s\n", l->info.codP);
printf("Codice categoria: %s\n", l->info.codC);
printf("Descrizione: %s", l->info.descr);
printf("Numero: %d\n", l->info.num);
printf("Costo: %.2f\n", l->info.costo);
l = l->next;
}
}
printf("\n");
}
void visualizzaMagazzino(listaC l) {
if(l == NULL) {
printf("Lista vuota. Nulla da visualizzare!\n");
}
else {
while(l != NULL) {
printf("Codice categoria: %s\n", l->codC);
l = l->next;
}
}
printf("\n");
}
void cancella_elemento(listaP *l, char codice_prodotto[], char codice_categoria[]) {
listaP pPrec;
listaP pCorrente;
pCorrente = *l;
if (pCorrente == NULL)
printf("\nLista vuota, nulla da cancellare\n");
else {
if (strcmp(pCorrente->info.codP, codice_prodotto) == 0 && strcmp(pCorrente->info.codC, codice_categoria) == 0) {
*l = pCorrente->next;
free(pCorrente);
printf("\nNodo cancellato in testa!\n");
}
else {
pPrec = *l;
pCorrente = (*l)->next;
while ((pCorrente != NULL) && ((strcmp(pCorrente->info.codP, codice_prodotto) != 0) || (strcmp(pCorrente->info.codC, codice_categoria) != 0))) {
pPrec = pCorrente;
pCorrente = pCorrente->next;
}
if(pCorrente == NULL) {
printf("\nElemento non trovato!\n");
}
else {
pPrec->next = pCorrente->next;
free(pCorrente);
printf("\nNodo cancellato in mezzo!\n");
}
}
}
}
listaC trovaCategoria(listaC lc, char categoria[]) {
listaC pCorrente = lc;
while(pCorrente != NULL){
if(strcmp(pCorrente->codC, categoria) == 0){
printf("\nCategoria già presente %s\n",categoria);
return pCorrente;
}
pCorrente = pCorrente->next;
}
return(NULL);
}
void insCat(listaC *lc, char cat[]) {
listaC nodoC;
listaC currC;
nodoC = malloc(sizeof(categoria));
strcpy(nodoC->codC, cat);
nodoC->nodoP= NULL;
nodoC->next = NULL;
if(*lc == NULL) {
*lc = nodoC;
}
else{
currC = *lc;
while(currC->next != NULL) {
currC = currC->next;
}
currC->next = nodoC;
}
}
int copiaMagazzino(listaC *lc, listaP l) {
listaC trovato;
int n = 0;
if(l == NULL) {
printf("\nLa lista dei prodotti è vuota!\n");
}
else {
while(l != NULL) {
trovato = trovaCategoria(*lc, l->info.codC);
if(trovato == NULL) {
insCat(lc, l->info.codC);
n++;
inserimentoProd((*lc)->nodoP, l->info);
}
else {
inserimentoProd(trovato->nodoP, l->info);
}
l = l->next;
}
}
return n;
}