Buffer Manipulation functions allow you to manipulate or compare characters stored in data buffers. These functions begin with the mem prefix. Expand the category to view a list of the available functions.
Function Name | Description |
memchr | Searches for a character in a buffer. |
memcmp | Compares two buffers. |
memcpy | Copies n characters from one buffer into another. |
memmove | Moves a number of bytes from one buffer to another. |
memset | Sets n bytes of a buffer to a given character. |
memchr
void *memchr(const void *s,int c,size_t n);
s: The name of the buffer.
c: The character to search for in the buffer.
n:The number of characters at the beginning of the buffer to search.
//以下的例子是在字符串"Example string"中搜索字符'x'出现的第一个位置
char * p;
char str[] = "Example xtring";
//获取第一个字符在内存中的位置
lr_output_message("The first character is at buffer %d",str);
//搜索整个字符串,获取'x'在内存中的位置
p = (char *)memchr(str, 'x', strlen(str));
if (p) {
//打印出获取'x'在内存中的位置
lr_output_message ("Character x is at buffer %d\n", p);
//打印出获取'x'在字符串中的位置,'x'的位置是内存中相对位置+1
lr_output_message ("Character x is at position %d\n", p - str + 1);
}
else
lr_output_message ("Character x was not found");
memcmp
int memcmp(const viod *s1,const void *s2,size_t n);
s1: The name of a buffer.
Se: The name of a buffer.
n:The number of characters to search at the beginning of the buffer.
//以下的例子是用memcmp来比较两个字符串是否相等。
char *s1="access to shared transmission";
char *s2="access to transmission";
int rc;
//通过memcmp返回值与0比较大写来确认字符串大小
rc =memcmp(s1,s2,strlen(s1));
if(rc>0)
lr_output_message("'%s' is greater than '%s'",s1,s2);//s1>s2
else if (rc<0)
lr_output_message("'%s' is less than '%s'",s1,s2);//s1<s2
else
lr_output_message("'%s' is equal to '%s'",s1,s2);//s1=s2
memcpy
Void *memcpy(void *dest,const void *src,size_t n);
dest: Destination buffer where the data is copied
src: The name of the source buffer from which from the data is copied.
n: The number of bytes to copy
char s1[] = "Sample string",s3[]="Just testing";
char s2[40],s4[40];
/* +1是为了复制s1的结束符号(NULLterminator) */
memcpy(s2, s1, strlen(s1)+1);
lr_output_message("s1=%s s2=%s", s1, s2);
/* -1使得s4比比s3少一位*/
memcpy(s4, s3, strlen(s3)-1);
lr_output_message("s3=%s s4=%s", s3, s4);
memmove
void *memmove(void *dest,const void *src,size_t n);
dest:Destination buffer where the data is copied.
src:The name of the source buffer from which the data is copied.
n:The number of bytes to copy.
char buffer[80];
strcpy(buffer,"abcde");
//把'abcde'的前四位'abcd'拼接到自身字符串从第二位开始的位置''
memmove(buffer+1,buffer,4);
lr_output_message("New string: %s\n",buffer);
memset
void *memset(void *buffer,int c,size_t n);
buffer:Pointer to block of data to be filled with c.
c:The character placed in buffer.
n:The number of bytes in buffer set as c.
//在初始化之前memset填充结构about_app中所有元素为0
//定义结构about_app
struct about_app {
char *version;
int field1;
int field2;
}a;
//赋值
a.field1=12;
a.field2=13;
//使用0填充所有的fields
memset(&a,0,sizeof(struct about_app));
//打印出所有的fields查看是否为0。
lr_output_message("After memset field1=%d field2=%d",a.field1,a.field2);
//初始化结构,并赋初值。
a.version="app version1.0";
a.field1=5;
a.field2=6;
lr_output_message("Now,field1=%d field2=%d",a.field1,a.field2);