《C和指针》——字符串操作补、字符串内存操作
来源:互联网 发布:农村淘宝官方下载 编辑:程序博客网 时间:2024/05/29 10:24
1.对于 *string++!='\0'
,它其实等价于:
*string!='\0'; string++;
2.对于 strcpy函数,如果要复制的字符串比老字符串短,由于新字符串是以NUL(即’\0’)结尾,所以老字符串最后剩余的几个字符也会被有效地删除。
如:
char message[ ]=="Original message";strcpy(message,"Different");
此时message=”Different”
3.同样使用strcpy和strcat时,目标字符数组的空间要足以容纳需要复制的字符串。
字符串 内存操作
这些函数都在#include <cstring>
1.void *memcpy(void *dst,void const *src,size_t length);
其实就是从src的起始位置复制length个元素到dst的内存起始位置
例子:
#include <stdio.h>#include <string.h>struct { char name[40]; int age;} person, person_copy;int main(){ char myname[] = "Pierre de Fermat"; /* using memcpy to copy string: */ memcpy(person.name, myname, strlen(myname) + 1); person.age = 46; /* using memcpy to copy structure: */ memcpy(&person_copy, &person, sizeof(person)); printf("person_copy: %s, %d \n", person_copy.name, person_copy.age); return 0;}
输出:
person_copy: Pierre de Fermat, 46
2.void *memmove(void *dst,void const *src,size_t length);
移动src开始的length个元素移动到dst开始的位置
例子:
/* memmove example */#include <stdio.h>#include <string.h>int main (){ char str[] = "memmove can be very useful......"; memmove (str+20,str+15,11); puts (str); return 0;}
输出:
memmove can be very very useful.
3.int *memcmp(void const *a,void const *b,size_t length);
a>b,返回值大于0
a=b,返回值等于0
a
/* memcmp example */#include <stdio.h>#include <string.h>int main (){ char buffer1[] = "DWgaOtP12df0"; char buffer2[] = "DWGAOTP12DF0"; int n; n=memcmp ( buffer1, buffer2, sizeof(buffer1) ); if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2); else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2); else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2); return 0;}
输出:
‘DWgaOtP12df0’ is greater than ‘DWGAOTP12DF0’.
4.void *memchr(void const *a,int ch,size_t length);
从a开始的length元素个数中找ch出现的第一个位置
例子:
/* memchr example */
#include <stdio.h>#include <string.h>int main(){ char * pch; char str[] = "Example string"; pch = (char*)memchr(str, 'p', strlen(str)); if (pch != NULL) printf("'p' found at position %d.\n", pch - str + 1); else printf("'p' not found.\n"); return 0;}
输出:
‘p’ found at position 5.
5.void * memset ( void * a, int ch, size_t length );
从a的开始的length元素都设置为值ch。
例子:
/* memset example */#include <stdio.h>#include <string.h>int main(){ char str[] = "almost every programmer should know memset!"; memset(str, '-', 6); puts(str); return 0;}
输出:
—— every programmer should know memset!
- 《C和指针》——字符串操作补、字符串内存操作
- 字符串操作和一级指针内存模型
- C语言内存操作函数和一般字符串操作函数
- C指针操作——获取字符串中所有数字
- 03字符串操作和一级指针内存模型
- C——常用字符串函数、内存操作函数总结
- Linux常用C函数—内存及字符串操作篇
- Linux常用C函数—内存及字符串操作篇
- Linux常用C函数—内存及字符串操作篇
- Linux常用C函数—内存及字符串操作篇
- 字符串操作&内存操作
- 字符串和内存操作函数
- 字符串操作与一级指针内存模型
- 通过指针如何操作字符串——储存和运算
- C指针操作——把ip字符串转换成ip range字符串
- C/C++ 内存块和字符串操作函数
- 读书笔记--C 指针实现各项字符串操作
- c字符串函数及指针操作
- Python序列基础
- vim-保存退出/其他操作
- 自定义按钮控件-热点按钮
- 生产者与消费者模式(线程的同步与互斥)
- 【个人笔记重点,不作为参考】主题:angular2.0 有关disabled的小问题
- 《C和指针》——字符串操作补、字符串内存操作
- Ubuntu Linux下安装软件方法
- Phpmyadmin报错:Error During Session Start; Please Check Your PHP And/Or Webserver Log File And Configu
- linux 下进入xampp中mysql
- C++第三次作业
- 浅析CDN技术与安全
- NYOJ1248_海岛争霸
- Cartographer记录
- leetcode python