strncpy和strcpy区别_C语言strncpy

strncpy和strcpy区别_C语言strncpyDefinedinheader <string.h>char *strncpy( char *dest, const ch

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

Defined in header 
<string.h>
   
  (1)  
char *strncpychar *dest, const char *src, size_t count );
(until C99)
char *strncpychar *restrict dest, const char *restrict src, size_t count );
(since C99)
errno_t strncpy_s(char *restrict dest, rsize_t destsz,
                  const char *restrict src, rsize_t count);
(2) (since C11)
     
1) Copies at most count characters of the character array pointed to by src (including the terminating null character, but not any of the characters that follow the null character) to character array pointed to by dest.
 If count is reached before the entire array src was copied, the resulting character array is not null-terminated.
 If, after copying the terminating null character from srccount is not reached, additional null characters are written to dest until the total of count characters have been written.
 The behavior is undefined if the character arrays overlap, if either dest or src is not a pointer to a character array (including if dest or src is a null pointer), if the size of the array pointed to by dest is less than count, or if the size of the array pointed to by src is less than count and it does not contain a null character.
2) Same as (1), except that the function does not continue writing zeroes into the destination array to pad up to count, it stops after writing the terminating null character (if there was no null in the source, it writes one at dest[count] and then stops). Also, the following errors are detected at runtime and call the currently installed constraint handler function:

  • src or dest is a null pointer
  • destsz or count is zero or greater than RSIZE_MAX
  • count is greater or equal destsz, but destsz is less or equal strnlen_s(src, count), in other words, truncation would occur
  • overlap would occur between the source and the destination strings
 The behavior is undefined if the size of the character array pointed to by dest < strnlen_s(src, destsz) <= destsz; in other words, an erroneous value of destsz does not expose the impending buffer overflow. The behavior is undefined if the size of the character array pointed to by src < strnlen_s(src, count) < destsz; in other words, an erroneous value of count does not expose the impending buffer overflow.

As with all bounds-checked functions, 
strncpy_s is only guaranteed to be available if 
__STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including string.h.

Parameters

dest pointer to the character array to copy to
src pointer to the character array to copy from
count maximum number of characters to copy
destsz the size of the destination buffer

Return value

1) returns a copy of dest
2) returns zero on success, returns non-zero on error. Also, on error, writes zero to dest[0] (unless dest is a null pointer or destsz is zero or greater than RSIZE_MAX) and may clobber the rest of the destination array with unspecified values.

Notes

As corrected by the post-C11 DR 468, strncpy_s, unlike strcpy_s, is only allowed to clobber the remainder of the destination array if an error occurs.

Unlike strncpystrncpy_s does not pad the destination array with zeroes, This is a common source of errors when converting existing code to the bounds-checked version.

Although truncation to fit the destination buffer is a security risk and therefore a runtime constraints violation for strncpy_s, it is possible to get the truncating behavior by specifying count equal to the size of the destination array minus one: it will copy the first count bytes and append the null terminator as always: strncpy_s(dst, sizeof dst, src, (sizeof dst)1);

Example

#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h> #include <stdio.h> #include <stdlib.h>   int main(void) { char src[] = "hi"; char dest[6] = "abcdef"; // no null terminator strncpy(dest, src, 5); // writes five characters 'h', 'i', '\0', '\0', '\0' to dest printf("strncpy(dest, src, 5) to a 6-byte dest gives : "); for(size_t n = 0; n < sizeof dest; ++n) { char c = dest[n]; c ? printf("'%c' ", c) : printf("'\\0' "); }   printf("\nstrncpy(dest2, src, 2) to a 2-byte dst gives : "); char dest2[2]; strncpy(dest2, src, 2); // truncation: writes two characters 'h', 'i', to dest2 for(size_t n = 0; n < sizeof dest2; ++n) { char c = dest2[n]; c ? printf("'%c' ", c) : printf("'\\0' "); } printf("\n");   #ifdef __STDC_LIB_EXT1__ set_constraint_handler_s(ignore_handler_s); char dst1[6], src1[100] = "hello"; int r1 = strncpy_s(dst1, 6, src1, 100); // writes 0 to r1, 6 characters to dst1 printf("dst1 = \"%s\", r1 = %d\n", dst1,r1); // 'h','e','l','l','o','\0' to dst1   char dst2[5], src2[7] = {'g','o','o','d','b','y','e'}; int r2 = strncpy_s(dst2, 5, src2, 7); // copy overflows the destination array printf("dst2 = \"%s\", r2 = %d\n", dst2,r2); // writes nonzero to r2,'\0' to dst2[0]   char dst3[5]; int r3 = strncpy_s(dst3, 5, src2, 4); // writes 0 to r3, 5 characters to dst3 printf("dst3 = \"%s\", r3 = %d\n", dst3,r3); // 'g', 'o', 'o', 'd', '\0' to dst3 #endif }

Possible output:

strncpy(dest, src, 5) to a 6-byte dst gives : 'h' 'i' '\0' '\0' '\0' 'f'
strncpy(dest2, src, 2) to a 2-byte dst gives : 'h' 'i'
dst1 = "hello", r1 = 0
dst2 = "", r2 = 22
dst3 = "good", r3 = 0

References

  • C11 standard (ISO/IEC 9899:2011):
  • 7.24.2.4 The strncpy function (p: 363-364)
  • K.3.7.1.4 The strncpy_s function (p: 616-617)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.21.2.4 The strncpy function (p: 326-327)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.11.2.4 The strncpy function

 

 

From: https://en.cppreference.com/w/c/string/byte/strncpy

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/168104.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • FLAG_ACTIVITY_CLEAR_TOP:「建议收藏」

    FLAG_ACTIVITY_CLEAR_TOP:「建议收藏」FLAG_ACTIVITY_CLEAR_TOP:例如现在的栈情况为:ABCD。D此时通过intent跳转到B,如果这个intent添加FLAG_ACTIVITY_CLEAR_TOP标记,则栈情况变为:AB。如果没有添加这个标记,则栈情况将会变成:ABCDB。也就是说,如果添加了FLAG_ACTIVITY_CLEAR_TOP标记,并且目标Activity在栈中已经存在,则将会把

    2022年7月17日
    23
  • 什么是java单例模式?[通俗易懂]

    什么是java单例模式?[通俗易懂]关于java单例模式的文章早已是非常多了,本文是对我个人过往学习java,理解及应用java单例模式的一个总结。此文内容涉及java单例模式的基本概念,以及什单例模式的优缺点,希望对大家有所帮助。什么是java单例模式?单例模式是保证类的实例是单例的一种常见设计模式。单例模式的优点:(1)首先肯定是节省内存资源,不管多频繁的通过暴露的方法创建实例,都能保证创建的对象在系统内存中是同一实例对象;(2)灵活性,由于所有实例的创建都由该类控制,所有该类可以灵活的更改实例化过程;(3)实例的

    2022年8月11日
    6
  • noip2015_noip2021复赛

    noip2015_noip2021复赛二项式定理推出系数等于a^n*b^m*C(n,k)快速幂+组合数(逆元做除法)结束。具体看代码:#include&lt;iostream&gt;#include&lt;cstdio&gt;#include&lt;cstring&gt;#include&lt;string&gt;#include&lt;algorithm&gt;#include&lt;vector&gt;#inc…

    2022年9月25日
    1
  • web图书销售管理系统_图书管理系统的主要功能有哪些?

    web图书销售管理系统_图书管理系统的主要功能有哪些?我们都已经了解到图书管理系统使用带来的好处,那么图书管理系统到底能给图书管理实现哪些功能。1、基础资料商品信息齐全,易学易用商品信息,供货商,客户,员工,仓库等基本参数的设置。支持一品多码管理。可快速检索商品,提高收银效率。可按岗位角色分组、分层、分级进行自定义权限管理。2、采购管理按需采购商商品,节省库存成本系统提出按需采购,市场拉动采购理念,减少库存积压,提高存货流转率。系统按照库存上下限来建…

    2022年6月11日
    37
  • [转载]下载网页中的ts视频文件

    [转载]下载网页中的ts视频文件目前来说,网上视频下载主要分为两大类,一类是大型正规网站,另一类就是第三方视频,基本上都是个人网站或者个人上传到网上的视频。第一类视频下载非常好办,只要下载安装官方客户端即可。第二类目前稍微复杂,目前主要的下载手段有通过浏览器视频嗅探插件、视频解析下载工具还有IDM嗅探下载。大家都知道,现在的视频网站播放技术主要以m3u8为主,这样的好处是用户点击暂停时即停止视频缓存,不造成宽带流浪占用浪费,同时也减轻了服务器的访问请求压力。这样做的坏处也很明显,嗅探软件不能嗅探完整的视频文件,嗅探到的也都是每

    2022年7月18日
    44
  • nginx面试常见问题_面试官应该问哪些问题

    nginx面试常见问题_面试官应该问哪些问题1.什么是Nginx?Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器目前使用的最多的web服务器或者代理服务器,像淘宝、新浪、网易、迅雷等都在使用2.为什么要用Nginx?优点:*跨平台、配置简单*非阻塞、高并发连接:处理2-3万并发连接数,官方…

    2022年8月27日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号