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
} // char *strncpy(char * __restrict__ _Dest,const char * __restrict__ _Source,size_t _Count) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
char string[10]={0};
char *string2 = "Hello World";
//_Count < sizeof(string) - 1
strncpy(string, string2, 2);
printf("%s\n", string); //He
memset(string, 0, sizeof(string));
//_Count > sizeof(string) - 1
strncpy(string, string2, strlen(string2));
printf("%s\n", string); // Hello Worldb 结果不可预测
memset(string, 0, sizeof(string));
//_Count = sizeof(string) - 1
strncpy(string, string2, sizeof(string) - 1);
printf("%s\n", string); //Hello Wor // 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;
}

文章参考

转载注明出处

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

  1. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  2. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  3. C标准库string.h中几个常用函数的使用详解

    strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...

  4. C 标准库 - string.h之strstr使用

    strstr Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not p ...

  5. C 标准库 - string.h之memmove使用

    memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...

  6. C 标准库 - string.h之memcpy使用

    memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...

  7. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

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

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

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

    strlen Returns the length of the C string str. The length of a C string is determined by the termina ...

随机推荐

  1. 13、Semantic-UI之表格与表单

    13.1 定义基础样式表格   在HTML中可以通过table进行表格定义,在Semantic-UI中也可以通过class="ui table"定义表格. 示例:定义基础表格 &l ...

  2. 底部导航栏使用BottomNavigationBar

    1.github地址 https://github.com/zhouxu88/BottomNavigationBar 2.基本使用 2,1添加依赖 implementation 'com.ashokv ...

  3. 介绍自己以及github注册流程

    我叫何季生,来自网络工程141,学号是1413042027,我喜欢看一些动漫和游戏,对于编程并不是很厉害希望今年能够有所突破. github注册流程:在刚开始注册github时,我用的是qq浏览器,却 ...

  4. leetcode 18 4Sum JAVA

    题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出 ...

  5. Mysql数据类型《四》日期类型

    日期类型 DATE TIME DATETIME TIMESTAMP YEAR 作用:存储用户注册时间,文章发布时间,员工入职时间,出生时间,过期时间等 YEAR YYYY(1901/2155) DAT ...

  6. “全栈2019”Java第三十二章:增强for循环Foreach语法

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  7. tcp/ip学习笔记(1)-基本概念

    为什么会有tcp/ip 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样.计 ...

  8. 洛谷P5245 【模板】多项式快速幂

    题面 传送门 题解 话说现在还用数组写多项式的似乎没几个了-- \[B(x)=A^k(x)\] \[\ln B(x)=k\ln A(x)\] 求个\(\ln\),乘个\(k\),\(\exp\)回去就 ...

  9. objectARX 添加线型下拉组合框空间 CAcUiLineTypeComboBox

    不知道是有意还是无意,objectARX的所有文档中,居然没有CAcUiLineTypeComboBox, 而实际上这个是存在的.位于\inc\acuiComboBox.h 而在添加变量的向导中也没有 ...

  10. 分享VMware题目解答

    VMnet1是主机模式.是一个Host-Only网络模式 192.168.1.254/24VMnet8是NAT模式.是一个NAT方式,最简单的组网方式VMnet6是手动设置的(主机.net.内部) 1 ...