문제

I wrote this function in C, which is meant to iterate through a string to the next non-white-space character:

char * iterate_through_whitespace(unsigned char * i){
    while(*i && *(i++) <= 32);
    return i-1;
}

It seems to work quite well, but I'm wondering if it is safe to assume that the *i will be evaluated to false in the situation that *i == '\0', and it won't iterate beyond the end of a string. It works well on my computer, but I'm wondering if it will behave the same when compiled on other machines.

도움이 되었습니까?

해결책

The standard says:

A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string.

다른 팁

Yes -- but in my opinion it's better style to be more explicit:

while (*i != '\0' && ...

But the comparison to 32 is hardly the best approach. 32 happens to be the ASCII/Unicode code for the space character, but C doesn't guarantee any particular character set -- and there are plenty of control characters with values less than 32 that aren't whitespace.

Use the isspace() function.

(And I'd never name a pointer i.)

In C, '\0' has the exact same value and type as 0. There is no reason to ever write '\0' except to uglify your code. \0 might however be useful inside double quotes to make strings with embedded null bytes.

The ASCII standard dictates that the NUL character is encoded as the byte 0. Unless you stop working with encodings that are backwards compatible with ASCII, nothing should go wrong.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top