1

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

    return 0;
}

size_t strlen(const char *s1)
{



    return s1 - 0;
}
blitzeus
  • 485
  • 2
  • 10
  • 28

1 Answers1

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;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523