0

Can someone please help me understand why when I try to print out the value of student_name, it only returns null? I'm implementing a basic hashtable in C to store the a students name, id, and 2 tests. Everything else is storing correctly, I just can't manage to save the the student_name no matter what I try. I have two structs, the hashtable itself and then record, the elements I intend to put inside of the table. The character string will never be longer than 18 characters.

int main(){
    char op[1];
    int stu_id[1];
    int exam1[1];
    int exam2[1];
    char * student_name = (char*)malloc(18*sizeof(char));

    struct hashtable * dictionary = malloc(sizeof(struct hashtable));
    dictionary->size = 13;
    dictionary->table = malloc(13*sizeof(struct record *));

    if(dictionary==NULL||dictionary->table==NULL){
        printf("Unable to allocate memory for the dictionary.\n");
        return;
    }

    int i;
    int s = 13;
    while(i<s){
        dictionary->table[i]=NULL;
        i++;
    }

    while(scanf("%s %d %d %d %s", op, stu_id, exam1, exam2, student_name) !=EOF){

        if(*op=='i'){
            printf("Intializing %s\n", *student_name);
            add_item(dictionary, stu_id[0], exam1[0], exam2[0], student_name);
    }
    free(dictionary);
    free(student_name);
    return 0;

}
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70

2 Answers2

1

Remember that a string always have to contain a special terminator character ('\0'). This means that a string of length one (like your op array) is actually two characters.

This means that when you read into op you are actually writing beyond the bounds of the array, leading to undefined behavior. You either need to increase the size of op (to at least two), or declare it as a single char (i.e. not an array) and use the '%c' format code to read a single character.

Also, don't declare the integer variables as arrays, use the address-of operator & when calling scanf instead:

char op;
int stu_id;
int exam1;
int exam2;

/* ... */

scanf("%c %d %d %d %s", &op, &stu_id, &exam1, &exam2, student_name)

You also should not check the return value of scanf against EOF, in case the input is not formatted correctly. Compare it agains the number of values you want scanned, five in your case.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • The issue is that student name is not getting assigned to the char * I've created for it and I have no idea why it's doing that. – Erica Fischer-Colbrie Sep 17 '13 at 03:00
  • @EricaFischer-Colbrie When you do e.g. `printf("Intializing %s\n", *student_name);` you tell `printf` to print a string, but `*student_name` returns the first *character*. It's undefined behavior and you should have gotten a warning about that. Remove the dereference operator `*` from `student_name`. – Some programmer dude Sep 17 '13 at 05:15
0

I suppose you are allocating memory for student record inside add_item() and assigning them to the dictionary->table. From the code you have posted, you are allocation memory to hold pointers to struct student record and not for the records themselves.

You need to free memory allocated for "dictionary->table" at the end of main().

Vivek S
  • 1,251
  • 10
  • 12