自己做网站和推广,网上家教网站开发,个人网页框架模板,替代wordpress1. C语言strlen函数参数如果是NULL#xff0c;则会出错。可以参考glibc中strlen的具体实现通常使用前可以判断一下参数是否是NULL#xff0c;或者自己写一个strlen的实现函数。2. String LengthYou can get the length of a string using the strlen function.This function …1. C语言strlen函数参数如果是NULL则会出错。可以参考glibc中strlen的具体实现通常使用前可以判断一下参数是否是NULL或者自己写一个strlen的实现函数。2. String LengthYou can get the length of a string using the strlen function.This function is declared in the header file string.h.Function: size_t strlen (const char *s)Parameterss: pointer to the null-terminated byte string to beexaminedReturn valueThe length of the null-terminated bytestring strThe strlen function returns the length of the string s inbytes. (In other words, it returns the offset of the terminatingnull byte within the array.)For example,strlen (hello, world)123. glibc中strlen的实现在本地的测试//// Name : mystrlen.cpp// Author :// Version :// Copyright : Yourcopyright notice// Description : Strlen in Chttps://github.com/lattera/glibc/blob/master/string/strlen.c//#include#include#includeusing namespace std;#undef strlen#ifndef STRLEN# define STRLEN strlen#endifsize_t STRLEN(const char *str) {const char *char_ptr;const unsigned long int *longword_ptr;unsigned long int longword, himagic, lomagic;for (char_ptr str;((unsigned long int) char_ptr (sizeof(longword) - 1)) !0;char_ptr)if (*char_ptr \0)return char_ptr - str;longword_ptr (unsigned long int *) char_ptr;himagic 0x80808080L;lomagic 0x01010101L;if (sizeof(longword) 4) {himagic ((himagic 16) 16) | himagic;lomagic ((lomagic 16) 16) | lomagic;}if (sizeof(longword) 8)abort();for (;;) {//如果传入的参数是空此处会访问崩溃出错//原因longword_ptr是NULL则*longword_ptr无法访问longword *longword_ptr;if (((longword - lomagic) ~longword himagic) ! 0){const char *cp (const char *) (longword_ptr - 1);if (cp[0] 0)return cp - str;if (cp[1] 0)return cp - str 1;if (cp[2] 0)return cp - str 2;if (cp[3] 0)return cp - str 3;if (sizeof(longword) 4) {if (cp[4] 0)return cp - str 4;if (cp[5] 0)return cp - str 5;if (cp[6] 0)return cp - str 6;if (cp[7] 0)return cp - str 7;}}}}//测试代码int main() {char *name NULL;int a;a strlen(name);//传入的参数如果是NULLreturn 0;}