3

I might be going insane, but I don't think I've ever seen this in c++ (though my reference code is in C). Why is there a static on the return value of the code here and what impact does it have? I don't think I've ever seen a static function outside class scope (but obviously C doesn't have classes and this probably has a different syntactical meaning).

/* just like strlen(3), but cap the number of bytes we count */
static size_t strnlen(const char *s, size_t max) {
    register const char *p;
    for(p = s; *p && max--; ++p);
    return(p - s);
}

From http://www.zeuscat.com/andrew/software/c/strnlen.c.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • It should be noted that the names like `strnlen` for your own functions are a very bad idea. `str*` is reserved by ISO C and POSIX if `string.h` is included, and in fact POSIX defines its own non-`static` `strnlen` which will conflict with this `static` one. – R.. GitHub STOP HELPING ICE Jan 17 '12 at 05:50
  • I'm not writing my own really, I just like to review things like this occasionally since interviewers frequently use this kind of question :) but thank you for the advice. – John Humphreys Jan 17 '12 at 14:44

1 Answers1

6

the static is not on the return type but on the function definition.

static functions don't have external linkage basically they are only visible to other functions in the same file.

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85