I have the following code snippets:
student_t students[10];
char *name = "Student";
and this one
for (int i = 0; i < STUDENT_COUNT; i++) {
students[i].id = i;
students[i].note = 1.0 + (i%4);
strcpy(students[i].name, name);
students[i].name[7] = (char) i + '0';
students[i].name[8] = '\0';
}
I dont understand this part:
students[i].name[7] = (char) i + '0';
students[i].name[8] = '\0';
What is happening here?
(char) i + '0'That converts anintsingle digit to an ascii character.0becomes'0',1becomes'1', etc.i(with'0' + i) and then terminating the string (with'\0').iis a number from0to9, then it can be represented by ASCII characters from'0'to'9'. And in order it to do so, it needs to get offset by the ascii value of'0'.STUDENT_COUNT? If it is greater than 9 then that code won't work.10instudent_t students[10];. Please do fully use your macros – there should be a single definition of sizes, limits etc where possible.