printf(rank);
is incorrect, as printf()
does not accept a single char
as input like that.
In C++, this code would not even compile at all. But in C, the compiler will implicitly convert an integral value into a pointer, and char
is an integral type.
The read Access Violation error is complaining about memory address 0x38
being read from. 0x38
is the ASCII code for the character '8'
, is that the value you are inputting?
The 1st parameter of printf()
must be a char*
pointer to a null-terminated format string, eg:
printf("%c", rank);
All of the examples in the code your college gave you are in this similar form.
If you remove the &
on the scanf("%c", &rank)
call (why?), you get an Access Violation writing to memory address 0x63
, because the value of rank
gets implicitly converted as-is to a pointer. 0x63
is the ASCII code for the character 'c'
, which is what you are initializing rank
to.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…