Skip to content

Instantly share code, notes, and snippets.

@klange
Created September 18, 2012 22:25
Show Gist options
  • Save klange/3746350 to your computer and use it in GitHub Desktop.
Save klange/3746350 to your computer and use it in GitHub Desktop.
strlen with no conditionals
#include <stdio.h>
typedef int (*strlen_f)(char *);
int plus_one(char *);
int ret_zero(char *);
int _strlen(char *);
strlen_f funcs[2] = {ret_zero, plus_one};
int plus_one(char * c) {
return 1 + _strlen(c+1);
}
int ret_zero(char * c) {
return 0;
}
int _strlen(char * s) {
return funcs[!!(*(s))](s);
}
int main(int argc, char * argv[]) {
printf("%s:%d\n", argv[1], _strlen(argv[1]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment