strncat, strncat_s
来自 zh.cppreference.net
| 定义于头文件 <string.h>
|
||
char* strncat( char* dest, const char* src, size_t count );
|
(1) | (直到 C99) |
char* strncat( char* restrict dest, const char* restrict src, size_t count );
|
(自 C99) | |
errno_t strncat_s( char* restrict dest, rsize_t destsz,
const char* restrict src, rsize_t count );
|
(2) | (自 C11) |
1) 最多追加
count 个来自指针 src 所指向的字符数组的字符,遇到空字符则停止,到指针 dest 所指向的空终止字节字符串的末尾。字符 src[0] 替换 dest 末尾的空终止符。最终总是添加终止空字符(因此函数最多可写入 count + 1 个字节)。 如果目标数组没有足够的空间来存放
dest 的内容和 count 的前 src 个字符以及终止空字符,则行为未定义。如果源和目标对象重叠,则行为未定义。如果 dest 不是指向空终止字节字符串的指针或 src 不是指向字符数组的指针,则行为未定义。2) 与 (1) 相同,除了此函数可能会破坏目标数组的剩余部分(从最后一个写入的字节到
destsz)并且以下错误会在运行时检测到并调用当前安装的 约束处理函数:
src或dest是空指针destsz或count为零或大于 RSIZE_MAX- ,在
destsz的前dest - 个字节中没有空字符,会发生截断:
count或src的长度中的较小者,超过了dest的空终止符与destsz之间的可用空间。 - 源字符串和目标字符串之间会发生重叠
如果
dest < strnlen(dest, destsz) + strnlen(src,count) + 1 < destsz 所指向的字符数组的大小;换句话说,错误的 destsz 值不会暴露即将发生的缓冲区溢出。如果 src < strnlen(src,count) < destsz 所指向的字符数组的大小;换句话说,错误的 count 值不会暴露即将发生的缓冲区溢出。 与所有边界检查函数一样,
strncat_s 只有在实现定义了 __STDC_LIB_EXT1__ 并且用户在包含 __STDC_WANT_LIB_EXT1__ 之前定义 1 为整数常量 <string.h> 时才能保证可用。参数
| dest | - | 指向要追加的空终止字节字符串的指针 |
| src | - | 指向要从中复制的字符数组的指针 |
| count | - | 要复制的最大字符数 |
| destsz | - | 目标缓冲区的大小 |
返回值
1) 返回
dest 的副本2)
dest[0] 成功时返回零,错误时返回非零。此外,在错误时,将零写入 dest(除非 destsz 是空指针或 为零或大于 RSIZE_MAX)。注解
因为 strncat 每次调用都需要查找到 dest 的末尾,所以使用 strncat 将多个字符串连接成一个的效率很低。
尽管为了适应目标缓冲区而进行的截断存在安全风险,因此对于 strncat_s 来说是一个运行时约束违规,但可以通过指定 count 等于目标数组大小减一来获得截断行为:它将复制前 count 个字节并像往常一样追加空终止符:strncat_s(dst, sizeof dst, src, (sizeof dst) - strnlen_s(dst, sizeof dst) - 1);
示例
运行此代码
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char str[50] = "Hello ";
char str2[50] = "World!";
strcat(str, str2);
strncat(str, " Goodbye World!", 3);
puts(str);
#ifdef __STDC_LIB_EXT1__
set_constraint_handler_s(ignore_handler_s);
char s1[100] = "good";
char s5[1000] = "bye";
int r1 = strncat_s(s1, 100, s5, 1000); // r1 is 0, s1 holds "goodbye\0"
printf("s1 = %s, r1 = %d\n", s1, r1);
char s2[6] = "hello";
int r2 = strncat_s(s2, 6, "", 1); // r2 is 0, s2 holds "hello\0"
printf("s2 = %s, r2 = %d\n", s2, r2);
char s3[6] = "hello";
int r3 = strncat_s(s3, 6, "X", 2); // r3 is non-zero, s3 holds "\0"
printf("s3 = %s, r3 = %d\n", s3, r3);
// the strncat_s truncation idiom:
char s4[7] = "abc";
int r4 = strncat_s(s4, 7, "defghijklmn", 3); // r4 is 0, s4 holds "abcdef\0"
printf("s4 = %s, r4 = %d\n", s4, r4);
#endif
}
可能的输出:
Hello World! Go
s1 = goodbye, r1 = 0
s2 = hello, r2 = 0
s3 = , r3 = 22
s4 = abcdef, r4 = 0
引用
- C23 标准 (ISO/IEC 9899:2024):
- 7.26.3.2 The strncat function (p: 379)
- K.3.7.2.2 The strncat_s function (p: TBD)
- C17 标准 (ISO/IEC 9899:2018):
- 7.24.3.2 The strncat function (p: 265-266)
- K.3.7.2.2 The strncat_s function (p: 449-450)
- C11 标准 (ISO/IEC 9899:2011):
- 7.24.3.2 The strncat function (p: 364-365)
- K.3.7.2.2 The strncat_s function (p: 618-620)
- C99 标准 (ISO/IEC 9899:1999):
- 7.21.3.2 The strncat function (p: 327-328)
- C89/C90 标准 (ISO/IEC 9899:1990):
- 4.11.3.2 The strncat function
参阅
(C11) |
连接两个字符串 (函数) |
(C11) |
将一个字符串复制到另一个 (函数) |
(C23) |
将一个缓冲区复制到另一个,在指定的分隔符后停止 (函数) |
C++ 文档 的 strncat
| |