zoukankan      html  css  js  c++  java
  • C语言 strncpy

    C语言 strncpy

    #include <string.h>
    char *strncpy(char *dest, const char *src, size_t n);

    功能:把src指向字符串的前n个字符复制到dest所指向的空间中,是否拷贝结束符看指定的长度是否包含''。
    参数:

    • dest:目的字符串首地址
    • src:源字符首地址
    • n:指定需要拷贝字符串个数

    返回值:

    • 成功:返回dest字符串的首地址
    • 失败:NULL

    案例

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    int main(void)
    {
        char ch[] = "hello world";
    
        // 字符串有限拷贝需要先初始化
        char str[100] = {0};
    
        // 字符串有限拷贝
        strncpy(str, ch, 5);
    
        printf("%s
    ", str);
    
        return 0;
    }
    strncpy 使用案例:使用函数
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    void my_strncpy(char* dest, const char* src,size_t n)
    {
        while ((*dest++ = *src++) && --n);
    }
    
    
    int main(void)
    {
        char ch[] = "hello world";
    
        // 字符串有限拷贝需要先初始化
        char str[100] = {0};
        my_strncpy(str, ch, 5);
    
        printf("%s
    ", str);
    
        return 0;
    }
    strncpy 使用案例:创建函数
  • 相关阅读:
    手机号不能为空
    选项卡套选项卡
    可以在一个html的文件当中读取另一个html文件的内容
    价格计算
    v形 加强版
    V形
    生成100个Div
    伪元素::after和::before
    数组中的toString,toLocalString,valueOf方法有什么区别
    JavaScript toLocaleString() 方法
  • 原文地址:https://www.cnblogs.com/xiangsikai/p/12378506.html
Copyright © 2011-2022 走看看