1

I want to rthe following error message......error: invalid conversion from ‘const char*’ to ‘size_t’</p>

    return 0;
}

size_t strlen(const char *s1)
{



    return s1 - 0;
}
4

1 回答 1

3

Subtracting zero from a pointer does not change the pointer, the same way that subtracting zero from a number does not change a number.

You should subtract the original pointer, not zero, to get the length:

size_t strlen(const char *s1) {
    const char *orig = s1;
    while (*s1) {
        s1++;
    }
    return s1 - orig;
}
于 2012-07-28T01:45:37.157 回答