I don't know how to check this function's correctness, but when I compare my code to other people I seem to have the idea right. Can someone tell me the correct implementation of this code?

// Determines whether a string represents a rest
bool is_rest(string s)
if (s = "\n")
    return true;

    return false;

First, you confuse = assign operator and == equals operator. This is still valid (though nonsensical) C code, as = in C forms an expression, and its value is the assigned value, so it can be tested for being non-zero by the if. And "\n", as a string constant, is the memory address of some two bytes {'\n','\0'} stored in a read-only area, so non-zero.

Then, you cannot compare strings with ==, since a string is a pointer to where the actual characters are stored, so you would compare the memory address, not the content. There's strcmp for this.

But third, the lines don't come with the newline operator in the string, so a rest is represented by a string of length zero, or equivalently, one starting with the null terminator.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .