首页 > 其他分享 >C标准库<string.h>-mem开头的函数

C标准库<string.h>-mem开头的函数

时间:2024-09-26 23:19:21浏览次数:3  
标签:string mem str2 str1 char int 内存 str 开头

void *memchr(const void *str, int c, size_t n)

函数功能

在参数str所指向的内存区域的前n个字节中搜索第一次出现字符c(转换为无符号字符类型)的位置。

参数解释

  1. str:指向要被搜索的内存区域的指针。
  2. c:要搜索的字符,以整数形式给出,但会被转换为无符号字符类型进行搜索。
  3. n:指定要搜索的字节数。

返回值

如果找到字符c,则返回指向字符c在内存区域中第一次出现位置的指针;如果在给定的内存区域中没有找到字符c,则返回一个空指针。

函数实现

函数用法

#include <stdio.h>
#include <string.h>
 
int main ()
{
   const char str[] = "http://www.runoob.com";
   const char ch = '.';
   char *ret;
 
   ret = (char*)memchr(str, ch, strlen(str));//原函数的返回值类型是“void *”,
//这里我们用强制类型转换将其变成一个“char *”,就可以输出“ch”及其之后的字符了
 
   printf("|%c| 之后的字符串是 - |%s|\n", ch, ret);
 
   return(0);
}
|.| 之后的字符串是 - |.runoob.com|

二、

#include <stdio.h>
#include <string.h>

int main() {
    const char *str = "Hello,World!";
    int c = 'W';
    int n = strlen(str);

    // 使用 memchr 查找字符 'W'
    char *result = (char *) memchr(str, c, n);

    if (result) {
        // 计算找到的字符在字符串中的位置
        int position = result - str+1;
        printf("Character '%c' found at position: %d\n", c, position);
    } else {
        printf("Character '%c' not found.\n", c);
    }

    return 0;
}
Character 'W' found at position: 7
请按任意键继续. . .

 int memcmp(const void *str1, const void *str2, size_t n)) 

函数功能

用于比较两个内存块(str1,str2),前n个字节的内容

参数解释

  • str1 -- 指向内存块的指针。
  • str2 -- 指向内存块的指针。
  • n -- 要被比较的字节数。

返回值

  • 如果返回值 < 0,则表示 str1 小于 str2。
  • 如果返回值 > 0,则表示 str1 大于 str2。
  • 如果返回值 = 0,则表示 str1 等于 str2。

函数实现



函数用法

通过“函数实现”我们可以知道这个函数对比两个字符串,当遇到不同的时候就做出判断,如果返回值 < 0,则表示 str1 小于 str2。如果返回值 > 0,则表示 str1 大于 str2。如果返回值 = 0,则表示 str1 等于 str2。

#include <stdio.h>
#include <string.h>

int main() {
    const char *str1 = "Hyllo";
    const char *str2 = "Horld";
    int n = 5; // 比较前5个字符

    int result = memcmp(str1, str2, n);

    if (result == 0) {
        printf("The memory areas are equal.\n");
    } else if (result < 0) {
        printf("The first memory area is less than the second.\n");
    } else {
        printf("The first memory area is greater than the second.\n");
    }

    return 0;
}
The first memory area is greater than the second.
请按任意键继续. . .

void *memcpy(void *str1, const void *str2, size_t n)

函数功能

memcpy 函数用于从源内存地址复制指定数量的字节到目标内存地址。

参数解释

  • str1:指向目标内存区域的指针,数据将被复制到这里。
  • str2:指向源内存区域的指针,数据将从这里复制。
  • n:要复制的字节数。

返回值

返回指向目标内存区域 str1 的指针。

函数实现

函数用法

#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "Hello, World!";
    char dest[20]; // 确保有足够的空间来存储复制的数据

    // 将 src 复制到 dest
    memcpy(dest, src, strlen(src) + 1); // 加1以包括空字符 '\0'

    printf("Source: %s\n", src);
    printf("Destination: %s\n", dest);

    return 0;
}
Source: Hello, World!
Destination: Hello, World!
请按任意键继续. . .

void *memmove(void *str1, const void *str2, size_t n)

函数功能

memmove 函数用于从源内存地址复制指定数量的字节到目标内存地址。与 memcpy 不同的是,memmove 可以处理源和目标内存区域重叠的情况。

参数解释

  • str1:指向目标内存区域的指针,数据将被复制到这里。
  • str2:指向源内存区域的指针,数据将从这里复制。
  • n:要复制的字节数。

返回值

返回指向目标内存区域 str1 的指针

函数实现

函数用法

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "abcdefghijklmn";
    // 将从 str + 2 开始的 5 个字节复制到 str + 7
    memmove(str + 7, str + 2, 5);
    printf("%s\n", str);
    return 0;
}
abcdefgcdefgmn
请按任意键继续. . .

void *memset(void *str, int c, size_t n)

函数功能

memset 函数用于将指定的值填充到内存区域的指定字节数中。这个函数通常用于初始化数组或清空内存区域。

参数解释

  • str -- 指向要填充的内存区域的指针。
  • c -- 要设置的值,通常是一个无符号字符。
  • n -- 要被设置为该值的字节数。

返回值

该值返回一个指向存储区 str 的指针。

函数实现

函数用法

#include <stdio.h>
#include <string.h>
 
int main ()
{
   char str[50];
 
   strcpy(str,"This is string.h library function");
   puts(str);
 
   memset(str,'$',7);
   puts(str);
   
   return(0);
}

This is string.h library function
$$$$$$$ string.h library function

标签:string,mem,str2,str1,char,int,内存,str,开头
From: https://blog.csdn.net/2401_82772407/article/details/142521580

相关文章

  • 易优CMS出现:Allowed memory size of 134217728 bytes exhausted (tried to allocate 2
    当你遇到“Allowedmemorysizeof134217728bytesexhausted(triedtoallocate20480bytes)”的错误时,这意味着PHP的内存限制已经耗尽。这种错误通常发生在处理大量数据或执行复杂计算时。为了解决这个问题,可以采取以下几种方法:方法1:修改 php.ini 文件(推荐)找到 php......
  • 易优CMS登录后台报Allowed memory size of 134217728 bytes ex hausted (tried to alo
    当你在登录后台时遇到“Allowedmemorysizeof134217728bytesexhausted(triedtoallocate20480bytes)”的错误提示时,通常是由于PHP的内存限制不足导致的。以下是一些具体的解决步骤:步骤1:检查PHP配置登录宝塔面板登录宝塔面板。在左侧菜单栏选择“软件商店”。......
  • 易优CMS为何我安装完提示这个报错?:Array and string offset access syntax with curly
    当你遇到类似 Arrayandstringoffsetaccesssyntaxwithcurlybracesisdeprecated 的报错时,通常是因为当前使用的PHP版本较高,而程序代码中使用了一些已弃用的语法。原因分析PHP版本过高:当前使用的PHP版本(如PHP7.4或更高版本)不再支持某些旧的语法形式。代码使......
  • .net 将string字符串转为json对象的两种方法
    1)将string直接转为json【注:适合信息量比较少的情况】  stringstr="{\"id\":\"s001\",\"name\":\"张三\",\"gender\":\"男\"}"    【注:上述中\起转义作用】2)将string信息转为list对象后再通过list对象转为json【注:适合信息量比较少的情况......
  • C++ 原始字符串(raw string literal )R“(...)“
    C++11可以在代码里嵌入一段原始字符串,该原始字符串不作任何转义,所见即所得。这个特性对于编写代码时要输入多行字符串,或者含带有特殊字符的字符串提供了巨大方便。语法:     R"(...)"记忆点:1.不做任何转义 stringstr="aaa\nbbb\nccc\n"; cout<<str<<endl......
  • tomcat8+memcached session共享
    一、环境准备时间同步(同步后确认各服务器时间是否一致,不一致需要修改一下时区)关闭防火墙软件包和jar包链接:https://pan.baidu.com/s/1sl9Nob7 二、安装配置nginx和memcachedNginx和memcached使用yum安装即可,下面是nginx配置文件内容usernginx;worker_processes1;events......
  • C++——输入一个字符串,把其中的字符按逆序输出。如输入LIGHT,输出THGIL。用string方法
    没注释的源代码#include<iostream>#include<string.h>usingnamespacestd;intmain(){   stringa;   cout<<"请输入字符串a:";   cin>>a;   intk;   k=a.size();   for(inti=k-1;i>=0;i--)   {       cout<<a[i];......
  • 07 字符串(str/string)-独有方法
    字符串是写代码中最常见python内存中的字符串是按照:unicode编码存储。字符串是不可变。独有方法.upper转换大写v='alex'v1=v.upper()print(v1)v2=v.isupper()#判断是否全部是大写print(v2).lower转换小写v='ALEX'v1=v.lower()print(v......
  • 08 字符串(str/string)-公共方法
    len计算长度计算字符长度#计算长度。(字符串->计算字符串中的字符个数)v="oldboy"print(len(v))index索引获取字符索引位置#索引取值(0作为开始)v="oldboy"v1=v[0]#0123...从前向后v2=v[-1]#-1-2-3...从后向前value='alexasdfasdfasdfasdfas......
  • 安装memcache集群管理工具
    安装memcache集群管理工具magent一、安装libeventtarxflibevent-2.0.20-stable.tar.gzcdlibevent-2.0.20./configure--prefix=/usr/local/libeventmake&&makeinstallecho"/usr/local/libevent/lib">/etc/ld.so.conf.d/libevent.confldco......