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. Mac安装Tomcat

    1. 到Tomcat官网下载,如下找tar格式文件: http://ftp.twaren.net/Unix/Web/apache/tomcat/tomcat-8/v8.0.41/bin/apache- ...

  2. C# DataGridView添加右键菜单的简单应用

    首先,参考了下以下文章: https://blog.csdn.net/qin_zhangyongheng/article/details/23773757 感谢. 项目中要在DataGridView中 ...

  3. ASP.NET Core2.0杂项

    一.添加自定义的路由. 1.1.默认的路由添加语句 Startup文件的ConfigureServices方法中添加: services.AddMvc();//添加MVC的服务 1.2 添加自定义的路 ...

  4. Windows上编译libjpeg

    通常libjpeg可以使用如下命令行生成Visual Studio 2010的项目文件: nmake /f makefile.vc setup-v10 但可惜我们使用的是Visual Studio 2 ...

  5. 「HNOI 2014」 画框

    题目链接 戳我 \(Solution\) 这一题很像最小乘积生成树.只是把\(kruskal\)变为了\(km\)/费用流 现在来讲一讲最小乘积生成树.首先将\(\sum a_i\)和\(\sum b ...

  6. HashMap源码解析 非原创

    Stack过时的类,使用Deque重新实现. HashCode和equals的关系 HashCode为hash码,用于散列数组中的存储时HashMap进行散列映射. equals方法适用于比较两个对象 ...

  7. OCP最新题库052考题解析及答案-第37题

    37.USER1 grants SELECT, INSERT, and UPDATE privileges on USER1. EMP to USER2. SYS executes this comm ...

  8. scrapy框架基础

    一.简介 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具有很强通用性 ...

  9. [ActionScript 3.0] flash中的颜色

    在 Flash 中,颜色就是一串特殊的数字,一个颜色值可以是0到16,777,215中的任意数值,这就是24位(bit)色彩.也许大家会问,为什么有16,777,216(256*256*256)种颜色 ...

  10. linux密码修改实验

    1.在单用户模式下进行引导 在不同的运行级别中,一个重要的运行级别就是单用户模式(运行级别1),该模式中,只有一个系统管理员使用特定的机器,而且尽可能少地运行系统服务,其中包含登录.单用户模式对少数管 ...