0

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?

7
  • 1
    (char) i + '0' That converts an int single digit to an ascii character. 0 becomes '0', 1 becomes '1', etc. Commented Nov 10, 2020 at 20:28
  • 1
    It's writing the character which represents the single digit value i (with '0' + i) and then terminating the string (with '\0'). Commented Nov 10, 2020 at 20:28
  • 1
    If i is a number from 0 to 9, 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'. Commented Nov 10, 2020 at 20:29
  • Can you please show the definition of STUDENT_COUNT? If it is greater than 9 then that code won't work. Commented Nov 10, 2020 at 20:30
  • Presumably that is the hard-coded 10 in student_t students[10];. Please do fully use your macros – there should be a single definition of sizes, limits etc where possible. Commented Nov 10, 2020 at 20:31

1 Answer 1

2

students[i].name[7] = (char) i + '0';

i + '0' converts the numbers 0 to 9 into the ASCII encoding of those numbers.[1]

  • 0'0'
  • 1'1'
  • ...
  • 9'9'

students[i].name[8] = '\0';

C strings must be terminated by a NUL character ('\0' aka just 0).


The buffer to which name points:

+-----+-----+-----+-----+-----+-----+-----+-----+
| 'S' | 't' | 'u' | 'd' | 'e' | 'n' | 't' |  0  |
+-----+-----+-----+-----+-----+-----+-----+-----+

students[i].name:

  1. Initial:

    +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+- ... -+-----+
    | ??? | ??? | ??? | ??? | ??? | ??? | ??? | ??? | ??? | ??? |       | ??? |
    +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+- ... -+-----+
    
  2. After strcpy(students[i].name, name);:

    +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+- ... -+-----+
    | 'S' | 't' | 'u' | 'd' | 'e' | 'n' | 't' |  0  | ??? | ??? |       | ??? |
    +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+- ... -+-----+
    
  3. After students[i].name[7] = (char) i + '0'; (when i==4):

    +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+- ... -+-----+
    | 'S' | 't' | 'u' | 'd' | 'e' | 'n' | 't' | '4' | ??? | ??? |       | ??? |
    +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+- ... -+-----+
    
  4. After students[i].name[8] = '\0';:

    +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+- ... -+-----+
    | 'S' | 't' | 'u' | 'd' | 'e' | 'n' | 't' | '4' |  0  | ??? |       | ??? |
    +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+- ... -+-----+
    

  1. On an EBCDIC machine, it will produce the EBCDIC encoding of the number.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.