-5
size_t strlen(const char* c)
{
  auto *c1 = c;
  while(*c1++);
  return c1 - c;
}

Mistakes I could find:
* missing NULL check for c
* c1 should be declared as char pointer (or is it required?)
* should typecast to size_t before returning

balajimc55
  • 2,192
  • 2
  • 13
  • 15

2 Answers2

4
  1. Off by 1: while(*c1++); --> while(*c1) c1++;

  2. "c1 should be declared as char pointer (or is it required?)" I would use const char *c1 = c;. Need to review if auto *c1 = c; is OK in C11.
    [Edit] Do not see that auto is valid in C11 either. Recommend const char *c1 = c;

  3. "should typecast to size_t before returning". return (size_t) (c1 - c); is a good idea as the pointer difference is type ptrdiff_t ( a signed integer type) and size_t is an unsigned integer type. This quiets warnings such as gcc -Wsign-conversion

  4. "missing NULL check for c" not needed as strlen(const char* c) expects pointer to a string and NULL is not a pointer to a string.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 2
    And 3."should typecast", not needed. +1 – ouah Nov 10 '15 at 21:44
  • 4. `auto` is still a storage modifier in C, not used for automatic variable type deduction, isn't it? – Kijewski Nov 10 '15 at 21:44
  • As you modified your answer to say the cast is *a good idea*, could you say why would it be a good idea to add a useless cast? – ouah Nov 10 '15 at 21:49
  • Omitting the type is never correct since c99. – ouah Nov 10 '15 at 21:51
  • 1
    @ouah Not good to add a useless cast. Yet IMO, not a useless cast. As `ptrdiff_t` is a signed type, casting to the unsigned type `size_t` can quiet pedantic warnings. – chux - Reinstate Monica Nov 10 '15 at 21:52
  • @ouah C11 added new semantics with `auto`. Unclear is OP's code is C11 compliant. IAC `const char *c1` works fine. – chux - Reinstate Monica Nov 10 '15 at 21:54
  • @chux C++ had a new `auto` semantic, I don't know any new `auto` semantic in C outside the one it had since C89. – ouah Nov 10 '15 at 21:55
  • @chux C does not require a diagnostic. Does your compiler give you a warning when you remove the cast? I mean even adding `-Wall -Wextra -std=c99 -pedantic`... – ouah Nov 10 '15 at 21:57
  • @ouah Agree about `auto`. Answer edited. – chux - Reinstate Monica Nov 10 '15 at 21:57
  • @ouah "warning: conversion to 'size_t' from 'int' may change the sign of the result [-Wsign-conversion]" – chux - Reinstate Monica Nov 10 '15 at 22:00
  • 1
    @chux ok my gcc version has a `-Wextra` that does not include `-Wsign-conversion` which is anyway an heresy IMHO. – ouah Nov 10 '15 at 22:04
  • Why is 3. needed if the return type is `size_t`? Just to hide a compiler warning? – juanchopanza Nov 10 '15 at 22:44
  • @juanchopanza The cast `(size_t)` is not needed, nor does the answer express a need. The answer expresses how it is useful. Code would be the same with or without the cast. It does quiet a warning by making explicit the conversion from a signed type to an unsigned one as implied conversions are a source of unexpected behavior. In this case code knows the difference in not ever negative so code is not trying to hide anything - but be explicit. Such warnings are often not enabled. – chux - Reinstate Monica Nov 10 '15 at 23:29
2

It looks like you will always have an answer 1 more than it should be. You need to subtract 1 or return c1 - c - 1;

Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30