views:

2854

answers:

2

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";
    }
}
+11  A: 

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.

+3  A: 

You return a const char *, ie a pointer to a location in memory where the string "o" is tored when you should be returning a char 'o'

shodanex