How to avoid segmentation fault with linked lists

I would like to make a code that uses linked list, takes input from the terminal and then print a table out.

In this example, I am uploading some information from element table.

I get segmentation error.

Somebody can help me to understand why?

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

typedef struct element{
    char name[20];
    char symbol[20];
    float atom_weight;
    struct Element* next;
} element;


/* Add a new node to the top of a list */
element* insert_top(char name[20], char symbol[20], float atom_weight, element* head) {
    element *new_element;
    new_element = (element *) malloc(sizeof(element));
    new_element->name[20] = name;
    new_element->symbol[20] = symbol;
    new_element->atom_weight = atom_weight;

    new_element->next= head;
    head = new_element;
    printf("Top inserted");
    return head;
}

element* table=NULL;
int main()
{
    int choice=1, i=0;
    char name[256];
    char symbol[256];
    float atom_weight;

    printf("%d", choice);
    while (choice!=0){

        printf("\n Please enter element name:");
        scanf("%s", name);

        printf("\n Please enter element symbol:");
        scanf("%s", symbol);

        printf("\n Please enter atomic weight:");
        scanf("%f", &atom_weight);


        //printf("%s, %s,...Weight %f",name, symbol, atom_weight);
        insert_top(name, symbol, atom_weight, table);

        i=i+1;
        printf("\nDo you want to continue (Y=1/N=0)? ");
        scanf("%d", &choice); //You should add the space before %c, not after

    }
    printf("Out of cycle\n");
    printf("Size of table %lu\n", sizeof(table));

    printf("Weight %f",table->atom_weight);


    while (table->next != NULL){
        printf("\nElement: %s \t\t Symbol: %s  \t\t Atomic weight: %f\n",table[i].name, table[i].symbol,table[i].atom_>
        //printf("ciao");
        table=table->next;
    }
}

Upvotes: 1

Views: 48

Answers (1)

&#212;rel
&#212;rel

Reputation: 7622

You need to copy strings using strcpy, see insert_top Element is not defined, should be element. Minor don't cast malloc result.

There is still some issues, char size limitation, scanf return code to be checked.

you need to free the malloc memory at the end.

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

typedef struct element{
    char name[20];
    char symbol[20];
    float atom_weight;
    struct element* next;
} element;


/* Add a new node to the top of a list */
element* insert_top(char name[20], char symbol[20], float atom_weight, element* head) {
    element *new_element = malloc(sizeof(element));
    strcpy(new_element->name, name);
    strcpy(new_element->symbol, symbol);
    new_element->atom_weight = atom_weight;

    new_element->next= head;
    printf("Top inserted");
    return new_element;
}

int main()
{
    element* table=NULL;
    int choice=1;

    printf("%d", choice);
    while (choice!=0){
        char name[256];
        char symbol[256];
        float atom_weight;

        printf("\n Please enter element name:");
        scanf("%s", name);

        printf("\n Please enter element symbol:");
        scanf("%s", symbol);

        printf("\n Please enter atomic weight:");
        scanf("%f", &atom_weight);


        //printf("%s, %s,...Weight %f",name, symbol, atom_weight);
        table = insert_top(name, symbol, atom_weight, table);

        printf("\nDo you want to continue (Y=1/N=0)? ");
        scanf("%d", &choice); //You should add the space before %c, not after

    }
    printf("Out of cycle\n");
    
    for (element *e = table; e; e = e->next) {
        printf("\nElement: %s \t\t Symbol: %s  \t\t Atomic weight: %f\n",
               e->name, e->symbol, e->atom_weight);
    }          
    return 0;  
}

Upvotes: 1

Related Questions