C 标准库 - string.h之strncpy使用

strncpy

  • 把 src 所指向的字符串复制到 dest,最多复制 n 个字符。当 src 的长度小于 n 时,dest 的剩余部分将用空字节填充。
char *strncpy(char *destination, const char *source, size_t num)

Parameters

destination

  • Pointer to the destination array where the content is to be copied.
  • 指向用于存储复制内容的目标数组。

source

  • C string to be copied.
  • 要复制的字符串。

num

  • Maximum number of characters to be copied from source.size_t is an unsigned integral type.
  • 要从源中复制的字符数。

Return Value

  • destination is returned.
  • 该函数返回最终复制的字符串。

复制 src 所指向的字符数组的至多 count 个字符(包含空终止字符,但不包含后随空字符的任何字符)到 dest 所指向的字符数组。

  • 若在完全复制整个 src 数组前抵达 count ,则结果的字符数组不是空终止的。
  • 若在复制来自 src 的空终止字符后未抵达 count ,则写入额外的空字符到 dest ,直至写入总共 count 个字符。
  • 若字符数组重叠,若 dest 或 src 不是指向字符数组的指针(包含若 dest 或 src 为空指针),若 dest 所指向的数组大小小于 count ,或若 src 所指向的数组大小小于 count 且它不含空字符,则行为未定义。

Example

//
// Created by zhangrongxiang on 2017/8/24 14:36.
// Copyright (c) 2017 ZRX . All rights reserved.
//
#include <stdio.h>
#include <string.h>

int main() {
    int i = 0;
    char destination[] = "********************"; // destination串为: "********************0"
    printf("strlen(destination) -> %d\n",strlen(destination)); //strlen(destination) -> 20
    const char *source = "-----";                // source串为:      "-----0"

    /**
     * C/C++中的strncpy()函数功能为将第source串的前n个字符拷贝到destination串,原型为:
     * 1、num<source串的长度(不包含最后的'\0'字符):
     * 那么该函数将会拷贝source的前num个字符到destination串中(不会自动为destination串加上结尾的'\0'字符);
     * 2、num=source串的长度(包含最后的'\0'字符):
     * 那么该函数将会拷贝source的全部字符到destination串中(包括source串结尾的'\0'字符);
     * 3、num>source串的长度(包含最后的'\0'字符):
     * 那么该函数将会拷贝source的全部字符到destination串中(包括source串结尾的'\0'字符),
     * 并且在destination串的结尾继续加上'\0'字符,直到拷贝的字符总个数等于num为止。
     */

    strncpy(destination, source, 5 );
//    -----***************
//    destination串为: "-----***************0"
    printf("%s\n",destination);
//
    strncpy( destination, source, 6 );
//    -----
//    destination串为: "-----0**************0"
    printf("%s\n",destination);

    strncpy(destination, source, 10);
//    destination串为: "-----00000**********0"
    printf("-> %s\n", destination);
    printf("sizeof(destination)%d\n", sizeof(destination));//21
    printf("--> %c\n", destination[sizeof(destination) - 2]);//*
    printf("--> %c\n", destination[strlen(destination) - 1]);//-

    for (; i < sizeof(destination); ++i) {
        printf("%d%c\t",i,destination[i]);
    }
//    0-      1-      2-      3-      4-      5       6       7       8       9       10*     11*     12*     13*     14*    15*     16*     17*     18*     19*     20
}

// A simple implementation of strncpy() might be:
    char *
    strncpy(char *dest, const char *src, size_t n)
    {
        size_t i;

        for (i = 0; i < n && src[i] != '\0'; i++)
            dest[i] = src[i];
        for ( ; i < n; i++)
            dest[i] = '\0';

        return dest;
    }

文章参考

原文地址:https://www.cnblogs.com/zhangrxiang/p/8404406.html

时间: 2024-12-12 17:42:18

C 标准库 - string.h之strncpy使用的相关文章

C 标准库 - &lt;string.h&gt;

C 标准库 - <string.h> 简介 string .h 头文件定义了一个变量类型.一个宏和各种操作字符数组的函数. 库变量 下面是头文件 string.h 中定义的变量类型: 序号 变量 & 描述 1 size_t 这是无符号整数类型,它是 sizeof 关键字的结果. 库宏 下面是头文件 string.h 中定义的宏: 序号 宏 & 描述 1 NULL这个宏是一个空指针常量的值. 库函数 下面是头文件 string.h 中定义的函数: 序号 函数 & 描述 1

C 标准库 - string.h之strcat使用

strcat Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatena

C 标准库 - string.h之strspn使用

strspn Returns the length of the initial portion of str1 which consists only of characters that are part of str2. The search does not include the terminating null-characters of either strings, but ends there. 检索字符串 dest 中第一个不在字符串 src 中出现的字符下标.返回 dest

C 标准库 - string.h之strpbrk使用

strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches. The search does not include the terminating null-characters of either str

C 标准库 - string.h之strlen使用

strlen Returns the length of the C string str. The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without

C 标准库 - string.h之memchr使用

memchr Locate character in block of memory,Searches within the first num bytes of the block of memory pointed by ptr for the first occurrence of ch (interpreted as an unsigned char), and returns a pointer to it. 在参数 ptr 所指向的字符串的前 count 个字节中搜索第一次出现字符

一、Python的标准库String

一.Python的标准库String 1.查看武器 a. help(type()) name = "jane"print(help(type(name))) b. capitalize() name = "jane" print(name.capitalize()) 效果:Jane c. center() name = "jane" print(name.center(50, '-')) 效果:-----------------------jan

实现C++标准库string类的简单版本

后续待更新. 1 #ifndef STRING_H 2 #define STRING_H 3 4 #include <cassert> 5 #include <utility> 6 #include <iostream> 7 8 namespace jz 9 { 10 11 /************************************************************************/ 12 /* 重新实现C风格字符串处理函数 */

【C++ Primer每日刷】之三 标准库 string 类型

标准库 string 类型 string 类型支持长度可变的字符串,C++ 标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与其他的标准库类型一样,用户程序要使用 string 类型对象,必须包含相关头文件.如果提供了合适的 using 声明,那么编写出来的程序将会变得简短些: #include <string> using std::string; 1.1 string 对象的定义和初始化 string 标准库支持几个