What does “Warning: return makes integer from pointer without a cast” mean in C?
-
16-09-2019 - |
Question
I'm writing a simple function in C whose purpose is to take a 6 bit number, work out the first 3 bits, and based on that, return a "r", "w" or "o".
However, when I compile I get this warning: 'return makes integer from pointer without a cast'. Then, when I run the program, I find that the function is returning a weird character that definitely isn't one of the three I'm after.
What is going on here? Thanks in advance.
Here's my function:
char
readorwrite(int opcode)
{
if (opcode >> 3 == 4) {
return "r";
} else if (opcode >> 3 == 5) {
return "w";
} else {
return "o";
}
}
Solution
The problem is that "r", "w" and "o" are string constants, which means that their actual type is char[] or char*. That means your function is actually returning s pointer to a zero-terminated string. For single characters, you should use 'r', 'w', and 'o' instead.
OTHER TIPS
You return a const char *
, ie a pointer to a location in memory where the string "o" is stored when you should be returning a char 'o'