'r'
is a single character which, when passed to a function, has type int
.
The function fopen()
expects the second argument to be a (const
) pointer to char (i.e. a const char *
). Hence the warning message. A conversion from an int
to a pointer is permitted in C, for various reasons, but compilers often treat such conversions as suspicious, and warn you.
The function expects the passed pointer to be a string, which means it assumes the value points to the first character of an array of char. It doesn't (since you have passed an int
) so exhibits undefined behaviour. One symptom of undefined behaviour is a segmentation violation (caused by your operating system detecting your program accessing memory it shouldn't, and sending a SIGSEGV
signal to your program).
To fix the problem pass "r"
instead of 'r'
. Note the double quotes rather than single quotes. "r"
is a string literal, which is represented using two characters, 'r'
and '\0'
, which is what fopen()
expects. Doing this will eliminate both the warning and the error when running the program.
Get in the habit of checking warning messages from your compiler BEFORE running the program. Warnings often indicate a potential problem - in this case, the cause of the warning is also the cause of the abnormal program exit.