曲径通幽论坛

标题: strncat() -- 连接两个字符串 [打印本页]

作者: beyes    时间: 2011-7-5 17:53
标题: strncat() -- 连接两个字符串
strncat() 声明如下:
[C++] 纯文本查看 复制代码
#include <string.h>
char *strncat(char *dest, const char *src, size_t n);

它和 strcat() 基本一样,它也是将 scr 指向的字符串连接到 dest 指向的字符串种,但有 2 点不同:

1. strncat() 最多只能使用 n 个 src 所指向的字符连接到 dest 中。

2. src 不要求以 null 结尾 。

示例代码
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>

int main(void)
{
    char str_1[128] = "hello";

    strncat (str_1, " world", 3);

    printf ("%s\n", str_1);

    return 0;
}

运行输出
$ ./strncat
hello wo





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2