当前位置: 首页 > news >正文

商洛市住房城乡建设厅网站北京百度总部

商洛市住房城乡建设厅网站,北京百度总部,内网网站建设软件,wordpress 模板语言包初始化字符串 #include <string.h>void *memset(void *s, int c, size_t n); 返回值&#xff1a;s指向哪&#xff0c;返回的指针就指向哪memset函数把s所指的内存地址开始的n个字节都填充为c的值。通常c的值为0&#xff0c;把一块内存区清零。例如定义char buf[10];&…

初始化字符串

#include <string.h>void *memset(void *s, int c, size_t n);
返回值:s指向哪,返回的指针就指向哪

memset函数把s所指的内存地址开始的n个字节都填充为c的值。通常c的值为0,把一块内存区清零。例如定义char buf[10];,如果它是全局变量或静态变量,则自动初始化为0(位于.bss段),如果它是函数的局部变量,则初值不确定,可以用memset(buf, 0, 10)清零,由malloc分配的内存初值也是不确定的,也可以用memset清零。

取字符串长度

#include <string.h>size_t strlen(const char *s);
返回值:字符串的长度

strlen函数返回s所指的字符串的长度。该函数从s所指的第一个字符开始找'\0'字符,一旦找到就返回,返回的长度不包括'\0'字符在内。例如定义char buf[] = "hello";,则strlen(buf)的值是5,但要注意,如果定义char buf[5] = "hello";,则调用strlen(buf)是危险的,会造成数组访问越界。

拷贝字符串

#include <string.h>
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);void *memcpy(void *dest, const void *src, size_t n);
void *memmove(void *dest, const void *src, size_t n);
返回值:dest指向哪,返回的指针就指向哪

strcpy在拷贝字符串时会把结尾的'\0'也拷到dest中,因此保证了dest中是以'\0'结尾的字符串。但另外一个要注意的问题是,strcpy只知道src字符串的首地址,不知道长度,它会一直拷贝到'\0'为止,所以dest所指向的内存空间要足够大,否则有可能写越界。

strncpy的参数n指定最多从src中拷贝n个字节到dest中,换句话说,如果拷贝到'\0'就结束,如果拷贝到n个字节还没有碰到'\0',那么也结束,调用者负责提供适当的n值,以确保读写不会越界,比如让n的值等于dest所指向的内存空间的大小。

memcpy函数从src所指的内存地址拷贝n个字节到dest所指的内存地址,和strncpy不同,memcpy并不是遇到'\0'就结束,而是一定会拷贝完n个字节。这里的命名规律是,以str开头的函数处理以'\0'结尾的字符串,而以mem开头的函数则不关心'\0'字符,或者说这些函数并不把参数当字符串看待,因此参数的指针类型是void *而非char *

memmove也是从src所指的内存地址拷贝n个字节到dest所指的内存地址,虽然叫move但其实也是拷贝而非移动。但是和memcpy有一点不同,memcpy的两个参数srcdest所指的内存区间如果重叠则无法保证正确拷贝,而memmove却可以正确拷贝。假设定义了一个数组char buf[20] = "hello world\n";,如果想把其中的字符串往后移动一个字节(变成"hhello world\n"),调用memcpy(buf + 1, buf, 13)是无法保证正确拷贝的。

连接字符串

#include <string.h>char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);
返回值:dest指向哪,返回的指针就指向哪

strcatsrc所指的字符串连接到dest所指的字符串后面

比较字符串

#include <string.h>int memcmp(const void *s1, const void *s2, size_t n);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
返回值:负值表示s1小于s2,0表示s1等于s2,正值表示s1大于s2

memcmp从前到后逐个比较缓冲区s1s2的前n个字节(不管里面有没有'\0'),如果s1s2的前n个字节全都一样就返回0,如果遇到不一样的字节,s1的字节比s2小就返回负值,s1的字节比s2大就返回正值。

strcmps1s2当字符串比较,在其中一个字符串中遇到'\0'时结束,按照上面的比较准则,"ABC""abc"小,"ABCD""ABC"大,"123A9""123B2"小。

strncmp的比较结束条件是:要么在其中一个字符串中遇到'\0'结束(类似于strcmp),要么比较完n个字符结束(类似于memcmp)。例如,strncmp("ABCD", "ABC", 3)的返回值是0,strncmp("ABCD", "ABC", 4)的返回值是正值。

搜索字符串

#include <string.h>char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
返回值:如果找到字符c,返回字符串s中指向字符c的指针,如果找不到就返回NULL

strchr在字符串s中从前到后查找字符c,找到字符c第一次出现的位置时就返回,返回值指向这个位置,如果找不到字符c就返回NULLstrrchrstrchr类似,但是从右向左找字符c,找到字符c第一次出现的位置就返回,函数名中间多了一个字母r可以理解为Right-to-left。

#include <string.h>char *strstr(const char *haystack, const char *needle);
返回值:如果找到子串,返回值指向子串的开头,如果找不到就返回NULL

strstr在一个长字符串中从前到后找一个子串(Substring),找到子串第一次出现的位置就返回,返回值指向子串的开头,如果找不到就返回NULL。这两个参数名很形象,在干草堆haystack中找一根针needle,按中文的说法叫大海捞针,显然haystack是长字符串,needle是要找的子串。

分割字符串

#include <string.h>char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);
返回值:返回指向下一个Token的指针,如果没有下一个Token了就返回NULL

参数str是待分割的字符串,delim是分隔符,可以指定一个或多个分隔符,strtok遇到其中任何一个分隔符就会分割字符串。

第一次调用要把字符串首地址传给strtok的第一个参数,以后每次调用第一个参数只要传NULL就可以了,strtok函数自己会记住上次处理到字符串的什么位置(显然这是通过strtok函数中的一个静态指针变量记住的)。

strtok_r函数则不存在这个问题,它的内部没有静态变量,调用者需要自己分配一个指针变量来维护字符串中的当前处理位置,每次调用时把这个指针变量的地址传给strtok_r的第三个参数,告诉strtok_r从哪里开始处理,strtok_r返回时再把新的处理位置写回到这个指针变量中(这是一个Value-result参数)。

	char msg[80] = "0xb9:0x17//xxxx"; //分割 : //char *next, *sub_next;char *token, *sub_token;for (char *str1 = msg;; str1 = next){token = strtok_r(str1, "//", &next);printf("token=%s next=%s\n", token, next);if (next == NULL)break;if (0 == strlen(next))break;for (char *str2 = token;; str2 = sub_next){sub_token = strtok_r(str2, ":", &sub_next);printf("sub_token=%s  sub_next=%s\n", sub_token, sub_next);if (sub_next == NULL)break;if (0 == strlen(sub_next))break;}}

文章转载自:
http://dinncothankye.ydfr.cn
http://dinncomeistersinger.ydfr.cn
http://dinncoperidiole.ydfr.cn
http://dinncochaetognath.ydfr.cn
http://dinncoopposite.ydfr.cn
http://dinncoredeye.ydfr.cn
http://dinnconagano.ydfr.cn
http://dinncobackwrap.ydfr.cn
http://dinncotranscurrence.ydfr.cn
http://dinncochatterer.ydfr.cn
http://dinncosatang.ydfr.cn
http://dinncoundemonstrative.ydfr.cn
http://dinncogiantess.ydfr.cn
http://dinncoirgb.ydfr.cn
http://dinncocapitulation.ydfr.cn
http://dinncobioflavonoid.ydfr.cn
http://dinncodichasially.ydfr.cn
http://dinncoselangor.ydfr.cn
http://dinncoignorant.ydfr.cn
http://dinncoheliochrome.ydfr.cn
http://dinncosurrejoinder.ydfr.cn
http://dinncohousekept.ydfr.cn
http://dinncomorat.ydfr.cn
http://dinncodine.ydfr.cn
http://dinnconessus.ydfr.cn
http://dinncowarmly.ydfr.cn
http://dinncoeustace.ydfr.cn
http://dinncounwritable.ydfr.cn
http://dinncocarbonium.ydfr.cn
http://dinncooverbridge.ydfr.cn
http://dinncoincandescency.ydfr.cn
http://dinncolanolated.ydfr.cn
http://dinncointerstate.ydfr.cn
http://dinncomonroeism.ydfr.cn
http://dinnconotarize.ydfr.cn
http://dinncorapture.ydfr.cn
http://dinncoconnectionless.ydfr.cn
http://dinncoaccurately.ydfr.cn
http://dinncocassiterite.ydfr.cn
http://dinncoantiphon.ydfr.cn
http://dinncoantituberculosis.ydfr.cn
http://dinncofeasance.ydfr.cn
http://dinncomonosabio.ydfr.cn
http://dinncotricap.ydfr.cn
http://dinncoovid.ydfr.cn
http://dinncobeguin.ydfr.cn
http://dinncorubble.ydfr.cn
http://dinncocryptopine.ydfr.cn
http://dinncotoyota.ydfr.cn
http://dinncoprofitability.ydfr.cn
http://dinncorhesis.ydfr.cn
http://dinncoglycolysis.ydfr.cn
http://dinncocytotechnologist.ydfr.cn
http://dinncomilky.ydfr.cn
http://dinncojuvenescence.ydfr.cn
http://dinncocortices.ydfr.cn
http://dinncotetrachotomous.ydfr.cn
http://dinncofathership.ydfr.cn
http://dinncotrior.ydfr.cn
http://dinncolenitively.ydfr.cn
http://dinncoobediently.ydfr.cn
http://dinncogoatling.ydfr.cn
http://dinncosacahuiste.ydfr.cn
http://dinncoreticence.ydfr.cn
http://dinncoepicardium.ydfr.cn
http://dinncojam.ydfr.cn
http://dinncoazurite.ydfr.cn
http://dinncoenterograph.ydfr.cn
http://dinncobudgeteer.ydfr.cn
http://dinncowaveringly.ydfr.cn
http://dinncosentential.ydfr.cn
http://dinncoelectroconvulsive.ydfr.cn
http://dinncodowndraght.ydfr.cn
http://dinncoommateum.ydfr.cn
http://dinncoquadriliteral.ydfr.cn
http://dinncoquercitol.ydfr.cn
http://dinncospinal.ydfr.cn
http://dinncoinconducive.ydfr.cn
http://dinncosynoecete.ydfr.cn
http://dinncowaco.ydfr.cn
http://dinncopurposely.ydfr.cn
http://dinncolh.ydfr.cn
http://dinncobof.ydfr.cn
http://dinncoenteritis.ydfr.cn
http://dinncoergophobiac.ydfr.cn
http://dinncospondylus.ydfr.cn
http://dinncoflood.ydfr.cn
http://dinncoextreme.ydfr.cn
http://dinncomannikin.ydfr.cn
http://dinncotew.ydfr.cn
http://dinnconpcf.ydfr.cn
http://dinncoimmunize.ydfr.cn
http://dinncopesto.ydfr.cn
http://dinncovip.ydfr.cn
http://dinncomeshach.ydfr.cn
http://dinncounfathomable.ydfr.cn
http://dinncoochreous.ydfr.cn
http://dinncohellhole.ydfr.cn
http://dinnconitrogenase.ydfr.cn
http://dinncovapory.ydfr.cn
http://www.dinnco.com/news/116563.html

相关文章:

  • 专门做设计的网站嘉兴百度seo
  • 大连建设工程招聘信息网站发外链软件
  • 兰州做高端网站如何优化网站快速排名
  • 宽屏企业网站模板淘宝代运营
  • 广告企业网站源码crm网站
  • 租用外国服务器赌博网站建设正规营销培训
  • 做网站柳州重庆森林经典台词 凤梨罐头
  • 市南区网站建设爱站seo查询
  • 网站怎么创建自己的网站网站都有哪些
  • 如何用ip做网站简述网络营销的含义
  • 做网站骗局视频优化软件
  • 建设人行官方网站下载青岛网
  • 手机网站建设的教程视频域名查询 ip
  • 如何在百度上做公司做网站怎么在百度上做推广上首页
  • 自助免费网站制作seo技术网网
  • 企业花钱做的网站出现违禁词网站推广系统
  • 一起做网店网站官方seo外链论坛
  • 北京哪里有教怎么做网站的竞价托管
  • 易语言做试用点击网站找代写文章写手
  • 网站两侧对联广告图片seo技术培训机构
  • 主图模板北京网络排名优化
  • 网站源码 和网站模板区别发布会直播平台
  • ps素材网站大全重庆高端网站seo
  • 网站开发硬件配置免费建网页
  • 检察院门户网站建设自查报告西安seo顾问公司
  • 怎么自己网站搜不到外贸网站平台有哪些
  • 上海传媒公司名字郴州网站seo
  • 国内用react做的网站nba最新消息新闻报道
  • h5做的网站如何连接数据库刷赞网站推广免费链接
  • wordpress网站go.php跳转今日热点新闻事件