C 标准库 - string.h之strncpy使用
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;
}
文章参考
- http://man7.org/linux/man-pages/man3/strncpy.3.html
- http://www.cplusplus.com/reference/cstring/strncpy/
- http://zh.cppreference.com/w/c/string/byte/strncpy
- http://www.runoob.com/cprogramming/c-function-strncpy.html
转载注明出处
C 标准库 - string.h之strncpy使用的更多相关文章
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
- C标准库<string.h>实现
本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...
- C标准库string.h中几个常用函数的使用详解
strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...
- 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 ...
- C 标准库 - string.h之memmove使用
memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...
- C 标准库 - string.h之memcpy使用
memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...
- C 标准库 - string.h之memcmp使用
memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...
- C 标准库 - string.h之memchr使用
memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...
- C 标准库 - string.h之strlen使用
strlen Returns the length of the C string str. The length of a C string is determined by the termina ...
随机推荐
- Python-面向对象编程01_什么是面向对象
Python从设计之初就已经是一门面向对象的语言了,正因如此,在Python中创建一个类和对象是很容易的. 什么是面向对象? 面向对象程序设计(Object-oriented programming, ...
- Android-ContentProvider理解操作系统的多媒体
在多年以前,我做一款音乐播放器,要把很多.mp3文件,放置在自己新建的文件夹里面,然后去读取这个新建的文件夹里面的音乐列表,现在想想是多么的无知: 因为只要往手机里面放入.mp3文件,系统会自动检测, ...
- 20145218张晓涵_Web基础
20145218张晓涵_Web基础 基础知识 Apache一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一.它快速.可靠并且 ...
- [LeetCode 题解]: Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Partition--分区切换2
有分区表TB2和TB2_History CREATE TABLE TB2( ID BIGINT IDENTITY(1,1) PRIMARY KEY, C1 NVARCHAR(200))ON[ps_T ...
- 基于python+selenium的框架思路
设想: 1.使用excel编写用例第一个sheet页为用例概要格式如下: 后面的sheet页为具体的用例步骤: 实现所有定位信息都与测试代码分离 2.读取该excel文件取出关键字等信息,作为关键字的 ...
- 萌新web前端从零开始(1)——计算机入门
前言:这是一个萌新从零开始的学习之路,与大家分享自己的看法与见解,还请指出错误与遗漏点方便改正. 1.认识计算机. 计算机语言常见的有C,PHP,Ruby,Java,C#,Basic,JS,C++等, ...
- BitArray简单例子
using System; using System.Collections; using System.Text; namespace TestConsole { class Program { s ...
- JAVA程序对MYSQL数据库加锁实验
什么是脏读,不可重复读,幻读 1. 脏读 :脏读就是指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个事务也访问这个数据,然后使用了这个数据. 2. 不可重 ...
- nowcoder(牛客网)OI测试赛2 解题报告
qwq听说是一场普及组难度的比赛,所以我就兴高采烈地过来了qwq 然后发现题目确实不难qwq.....但是因为蒟蒻我太蒻了,考的还是很差啦qwq orz那些AK的dalao们qwq 赛后闲来无事,弄一 ...