#include <string.h>
char *strncpy(char *dest, const char *src, size_t n);

功能:把src指向字符串的前n个字符复制到dest所指向的空间中,是否拷贝结束符看指定的长度是否包含'\0'。
参数:

  • dest:目的字符串首地址
  • src:源字符首地址
  • n:指定需要拷贝字符串个数

返回值:

  • 成功:返回dest字符串的首地址
  • 失败:NULL

案例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    char ch[] = "hello world";

    // 字符串有限拷贝需要先初始化
    char str[100] = {0};

    // 字符串有限拷贝
    strncpy(str, ch, 5);

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

    return 0;
}
strncpy 使用案例:使用函数

相关文章:

  • 2022-01-12
  • 2021-11-29
  • 2022-12-23
  • 2022-01-16
  • 2021-08-21
  • 2022-12-23
  • 2022-01-17
  • 2021-12-20
猜你喜欢
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案