自定义strlen()

来源:互联网 发布:血源诅咒帅哥捏脸数据 编辑:程序博客网 时间:2024/06/15 21:17

自定义strlen()

源代码

/* * 自定义strlen()函数 * */#include <stdio.h>#include <assert.h>#include <string.h>int my_strlen(const char *str){    int count = 0;    assert(NULL != str);    while (*str++ != '\0')        count++;    return count;}int main(void){    char str[] = "hello world";    char *ptr = NULL;    printf("   strlen(str) : %d\n", (int)strlen(str));    printf("my_strlen(str) : %d\n", my_strlen(str));    printf("my_strlen(ptr) : %d\n", my_strlen(ptr));    return 0;}

运行结果

[root@localhost lwp_workspace]# ./test    strlen(str) : 11my_strlen(str) : 11test: my_strlen.c:14: my_strlen: Assertion `((void *)0) != str' failed.已放弃[root@localhost lwp_workspace]# 
0 0