0

Working on homework in C Programming with Code::blocks for making a list of students with add, remove, sort and search capabilities, I wrote a function for searching students based on student code as follow :

void search_student(struct student students[],int n)
{
    char temp_code[11];
    int row = 0;
    printf("Please enter the student code to search: ");
    scanf ("%s", temp_code);
    printf("\n#\tFirst_Name\tLast_Name\tStudent_Code\tAverage\n");
    puts("------------------------------------------------------------------");
    for (int i = 0 ; i < n ; i++)
    {
        printf("temp code= %s  --- students[%d].first_name = %s\n", temp_code, i, students[i].student_code);
        if (strcmp(temp_code, students[i].student_code) == 0 )
        {
            printf("%d\t%s\t\t%s\t\t%s\t%f\n",
                i+1, students[i].first_name, students[i].last_named, students[i].student_code, students[i].average);
            row = i + 1;
        }
    }
    if (row == 0)
    {
        puts("\nThe Student Code does not exist!\n");
    }
}

the output of first fprint in for loop showed the following output:

Please enter the student code to search: student0002

#       First_Name      Last_Name       Student_Code    Average
------------------------------------------------------------------
temp code= student0002  --- students[0].student_code = student0001
temp code= student0002  --- students[1].student_code = student0002
temp code= student0002  --- students[2].student_code = student0003

The Student Code does not exist!

it seems after first loop, strcmp adds a character to end of temp_code which causes wrong output!!!

I found that manipulating irrelevant parts of the code can solve this problem!! (for example removing row = i+1 after second printf in for loop) and of course using strncmp can work nice!! anyway I couldn't figure out why strcmp acts like this!!

peacekeeper
  • 45
  • 1
  • 6
  • 6
    Your array temp_code is too short to store a string of 11 characters. An array of 11 yes, but not a string of length 11. – FredK Oct 18 '18 at 21:12
  • 1
    No, `strcmp` isn't mysterious or broken. Strings in C require a termination 0 (NULL) character. So you need 12 chars to hold a string of length 11. – lurker Oct 18 '18 at 21:14
  • https://stackoverflow.com/questions/2430303/disadvantages-of-scanf – Hans Passant Oct 18 '18 at 21:39
  • It means you are writing where you're not supposed to be. "Fortunately", IIRC, you're writing the NULL character in `row` content, because it follows `temp_code` and is statically allocated. – Amessihel Oct 18 '18 at 21:45
  • Lesson learned -- don't skimp on array or buffer sizes. You would rather be `1,000` characters *too long* than `1` character *too short*. Additionally, your "search for student code" looks like it is just dumping `i = 0; i < n` nodes from the list without iterating over the full list matching student code -- is this what you intended? – David C. Rankin Oct 18 '18 at 22:09

1 Answers1

3

temp_code buffer is not big enough for the string read. This causes your program to have Undefined Behavior. That's why you see strange unexplained behavior.

sizeof("student0002") == 12 because in C strings require a null-terminating character.

Make temp_code large enough for any reasonable input. E.g. char temp_code[2048]; or look into solutions to prevent buffer overflowing like this: How to prevent scanf causing a buffer overflow in C?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
bolov
  • 72,283
  • 15
  • 145
  • 224