Symbol Table Code in C: "segmentation fault (core dumped)" error with Insert function

83 views Asked by At

For this assignment we were given a C file of symbol table code (https://forgetcode.com/C/101-Symbol-table) in which we had to remove the character array "label" and turn symbol from char[] to char *. We were also instructed to only take in input in the main function; taking in input from inside any other function is considered incorrect. I've only worked with data structures in Java, so it's possible that there are different syntax rules or something in C, but I genuinely don't know what's causing the error. Edit: I rewrote some of the code and it's still giving me the same error.

#include<stdio.h>
/* #include<conio.h> */
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
int size=0;
void Insert();
void Display();
void Delete(); // Prototypes declared since functions are declared after our main function.
int Search(char lab[]);
void Modify();

struct SymbTab { 
    char* symbol;
    int addr;
    struct SymbTab *next;};

struct SymbTab *first,*last; //defines two pointers that are automatically set to NULL.

void main() { // Main function: the only function that takes in input.
    int op,y; // Variable declarations.
    int addrs;
    char symbl[30];
    do { 
        printf("\n\tSYMBOL TABLE IMPLEMENTATION\n");
        printf("\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.SEARCH\n\t5.END\n");
        printf("\n\tEnter your option : ");
        scanf("%d",&op); // Asks for input.
        switch(op) { // All possible responses to whatever the user inputs.
        case 1: // Calls the Insert case.
            printf("Insert symbol: ");
            fgets(symbl, sizeof(symbl), stdin);
            scanf("%s", symbl); // Takes user input and stores in symbol.
            printf("Insert address:");
            scanf("%d", addrs); // Takes user input and stores in addr.
            Insert(symbl, addrs); // Passes symbol and addr (which now contain the user's input) as parameters into the Insert function.
            break; // Exists the Insert case and reruns starting at the beginning of the "do".
        case 2: // Calls the Display case.
            Display(); // Calls the Display function.
            break; // Exists the Display case and reruns starting at the beginning of the "do".
        case 3: // Calls the Delete case.
            printf("\n\tEnter the symbol to be deleted: %s", symbl);
            y = Search(symbl);
            if(y == 0)
                printf("\n\tLabel not found\n");
                break;
            if (y == 1) 
                Delete(symbl);
                break; // Exists the Delete case and reruns starting at the beginning of the "do".
        case 4: // Calls the Search case.
            scanf("\n\tEnter the label to be searched: %s", symbl);
            y=Search(symbl);
            printf("\n\tSearch Result:");
            if(y==1)
                printf("\n\tThe label is present in the symbol table\n");
            else
                printf("\n\tThe label is not present in the symbol table\n");
            break; // Exists the Search case and reruns starting at the beginning of the "do".
        case 5: // If the user's input is invalid/doesn't match with any of the existing functions, the program exits/ends.
            exit(0);
            }
    }
    while(op<5);

}  /* and of main */

// Precondition: Two parameters: a key (string of characters) and an address (int).
void Insert(char * symbl, int address) {
    struct SymbTab *p;
    p=malloc(sizeof(struct SymbTab));
    strcpy(p->symbol,symbl);
    p->next=NULL;
    if (size==0) {
        first=p;
        last=p;
    }
    else
    {
        last->next=p;
        last=p;
    }
    size++;
printf("\n\tLabel inserted\n");
    }

// Postcondition: Variable (with it's corresponding address) are added into the symbol table, and print statement is displatyed on the screen.

Any time I try to run my Insert function, it gives me a "segmentation fault (core dumped)" error. I've tried to declare/assign values into my linked list in different ways, but nothing seems to work.

0

There are 0 answers