Home > Enterprise >  Why doesn't my C struct get any data in input?
Why doesn't my C struct get any data in input?

Time:10-31

So, I was trying to write this program in which it should be possible to create a dynamic list in which are stored data about some cars (model, colour, year) and then it should be possible to view a list of all of them. There are no errors in the program, and no segmentation fault, however as soon as I try to visualize the list, I get no output. I tried using GDB to debug it, and the struct pointer doesn't actually get any data during the input. Here is the whole code:

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


struct node{
    char modello[81];
    char colore[81];
    int anno;
    struct node *next;
};
typedef struct node car;


car* insertNode(car* head);
car* destroyer(car* head);
void visualizer(car *head);

int main(){
    car *head = NULL;
    int n,tasto;
    char option,enter;
    
    do{
        printf("Premere 1 per inserire un nuovo veicolo nel catalogo.\n");
        printf("Premere 2 per visualizzare l'intero catalogo.\n");
        printf("Premere qualsiasi altro tasto per uscire.\n\n");
        scanf("%i",&tasto);
        switch (tasto){
            case 1:
                insertNode(head);
                break;
            case 2:
                visualizer(head);
                break;
            default:
                break;
        }
    }while(tasto==1||tasto==2);
    
    if(head!=NULL)
        head=destroyer(head);
    printf("Uscita.\n");
    
    return 0;
}

car* insertNode(car *head){
    car *temp;
    car *prec;
    temp=(car *)malloc(sizeof(car));
    
    if(temp!=NULL){
        temp->next=NULL;
        if(head==NULL)
            head=temp;
        else{//Raggiungi il termine della lista
            for(prec=head;(prec->next)!=NULL;prec=(prec->next));
            prec->next=temp;
        }
        printf("Inserire il modello dell'auto: ");
        scanf("%s",&temp->modello);
        printf("Inserire il colore dell'auto: ");
        scanf("%s",&temp->colore);
        printf("Inserire l'anno di immatricolazione dell'auto: ");
        scanf("%i",&temp->anno);
        printf("\n");
    }
    else
        printf("Memoria esaurita!\n");
    
    return head;
}

void visualizer(car* head){
    car *temp;
    int i=1;
    temp=head;
    while(temp!=NULL){
        printf("Auto numero %i:\n",i);
        printf("Modello: %s.\n",temp->modello);
        printf("Colore: %s.\n",temp->colore);
        printf("Anno di immatricolazione: %i.\n",temp->anno);
        printf("\n");
        i  ;
        temp=temp->next;
    }
    
}

car *destroyer(car* head){
    car *temp;
    while(head!=NULL){
        temp=head;
        head=head->next;
        free(temp);
    }
    
    return NULL;
}

Can someone please explain why this happens? I have no clue about what's wrong with this.

CodePudding user response:

The first error is in the do while, when you are at about line 31 in the case switch you don't store the return of the insert function anywhere. Here it is the fixed code:

do{
        printf("Premere 1 per inserire un nuovo veicolo nel catalogo.\n");
        printf("Premere 2 per visualizzare l'intero catalogo.\n");
        printf("Premere qualsiasi altro tasto per uscire.\n\n");
        scanf("%i",&tasto);
        switch (tasto){
            case 1:
                head=insertNode(head);
                break;
            case 2:
                visualizer(head);
                break;
            default:
                break;
        }
    }while(tasto==1||tasto==2);

The second error is when you are getting the value from keyboard in insert node. You are using the "&" operator when you already have the address of the array you want to fill, because you are indeed working with arrays. Here you can see the fixed code:

car* insertNode(car *head){
    car *temp;
    car *prec;
    temp=(car *)malloc(sizeof(car));
    
    if(temp!=NULL){
        temp->next=NULL;
        if(head==NULL)
            head=temp;
        else{//Raggiungi il termine della lista
            for(prec=head;(prec->next)!=NULL;prec=(prec->next));
            prec->next=temp;
        }
        printf("Inserire il modello dell'auto: ");
        scanf("%s",temp->modello);
        printf("Inserire il colore dell'auto: ");
        scanf("%s",temp->colore);
        printf("Inserire l'anno di immatricolazione dell'auto: ");
        scanf("%i",&temp->anno);
        printf("\n");
    }
    else
        printf("Memoria esaurita!\n");
    
    return head;
}
  • Related