strncpy基本用法
见百度百科。
注意这句话:
(c/c++)复制src中的内容(字符,数字、汉字....)到dest,复制多少由num的值决定,返回指向dest的指针。如果遇到null字符('\0'),且还没有到num个字符时,就用(num - n)(n是遇到null字符前已经有的非null字符个数)个null字符附加到destination。注意:并不是添加到destination的最后,而是紧跟着由source中复制而来的字符后面。
示例程序:
#include <cstdio>
#include <cstring>
#include <cstdlib>
int main(void)
{
// char *greeting = "hello";
// char *temp = (char*)malloc(6);
// strncpy(temp, greeting, 3);
// strncpy(temp + 3, "p!", 2);
// greeting = temp;
// printf("%s\n", greeting);
char des[] = "Hello,i am!";
int len = strlen(des);
char source[] = "abc\0def";
strncpy(des, source, );
printf("%s\n", des);
; i < len; ++i) {
if (des[i] == '\0') printf("$");
else printf("%c", des[i]);
}
printf("\n");
;
}
可以发现输出是:
abc$$,i am!
这正是我们想要的==
strncpy基本用法的更多相关文章
- strncpy的用法
strncpy是C语言的库函数之一,来自C语言标准库,定义于string.h,函数原型是: char *strncpy(char* dest,char* src,size_t n); 把src所指向的 ...
- C++ strcpy strcpy_s strncpy strlcpy
strncpy的用法:它与strcpy的不同之处就在于复制n个字符,而不是把所有字符拷贝(包括结尾'\0'). 函数原型:char * strncpy(char *dst,const char * s ...
- C/C++常见问题汇总
问题1.数组和指针的区别 数组名不可以作为左值 char * p1 = "Hello World" ; //分配字符串常量,然后赋给 p1 ,一个指针型变量,是左值 ] = &qu ...
- sizeof strlen strncpy用法总结 结构体实际所占内存大小 以及memset用法
sizeof测类型(数组名除外) strlen测实际长度 strncpy返回指针类型 #include <stdio.h> #include <stdlib.h> #inclu ...
- strncpy 用法
strncpy 用法 原型:extern char *strncpy(char *dest, char *src, int n); 用法:#include <string.h> 功能: ...
- C语言中函数strcpy ,strncpy ,strlcpy的用法【转】
转自:http://blog.chinaunix.net/uid-20797562-id-99311.html strcpy ,strncpy ,strlcpy的用法好多人已经知道利用strncpy替 ...
- (C)strcpy ,strncpy和strlcpy的基本用法
好多人已经知道利用strncpy替代strcpy来防止缓冲区越界. 但是如果还要考虑运行效率的话,也许strlcpy是一个更好的方式. 1. strcpy strcpy 是依据 /0 作为结束判断的, ...
- C语言中函数strcpy ,strncpy ,strlcpy的用法
strcpy ,strncpy ,strlcpy的用法 好多人已经知道利用strncpy替代strcpy来防止缓冲区越界. 但是如果还要考虑运行效率的话,也许strlcpy是一个更好的方式. 1. s ...
- C语言——常用标准输入输出函数 scanf(), printf(), gets(), puts(), getchar(), putchar(); 字符串拷贝函数 strcpy(), strncpy(), strchr(), strstr()函数用法特点
1 首先介绍几个常用到的转义符 (1) 换行符“\n”, ASCII值为10: (2) 回车符“\r”, ASCII值为13: (3) 水平制表符“\t”, ASCII值为 9 ...
随机推荐
- linux复习
linux的特点 - 免费的/开源 - 支持多线程/多用户 - 安全性好 - 对内存和文件管理优越 关机命令 ...
- iOS MD5加密字符串
参考:http://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c 在线测试MD5:http://www.cmd5.c ...
- 【转载】CSS规范
原文地址:http://www.cnblogs.com/whitewolf/p/4491707.html 目录 HTML 语法 HTML5 doctype 语言属性(Language attribut ...
- STL中的查找算法
STL中有很多算法,这些算法可以用到一个或多个STL容器(因为STL的一个设计思想是将算法和容器进行分离),也可以用到非容器序列比如数组中.众多算法中,查找算法是应用最为普遍的一类. 单个元素查找 1 ...
- zabbix3.0.4 部署之二 (Centos6.5系统准备)
1.安装Centos6.5 2.6.32-642.4.2.el6.x86_64 升级所有软件至最新: yum update 2.同步时间.安装ntpd yum install ntpddate n ...
- 使用spark与ElasticSearch交互
使用 elasticsearch-hadoop 包,可在 github 中搜索到该项目 项目地址 example import org.elasticsearch.spark._ import org ...
- AS3绘制扇形算法解析
网上有很多使用AS3画一个扇形的方法,但是却一个都没有解释这个函数是如何运作来画出扇形的,下面浅谈下我对这个函数的理解. 首先上代码,代码来自http://blog.csdn.net/weiming8 ...
- vs---错误收集并自己解决后归纳
1.C++编译时,出现这样的错误 d:\program files\microsoft visual studio\vc98\include\stdio.h(36) : error C2143: sy ...
- Yii 提示Invalid argument supplied for foreach() 等错误
Yii 提示Invalid argument supplied for foreach() 或者 undefined variable: val等错误 只需要在对应的文件中加入error_report ...
- python 多线程和多核
昨天在一个群里面遇到的,使用py做计算,只有1个CPU在跑任务,其它的怠工. 在py3以后,提供了:concurrent.futures mark 如下; 官方文档: 16.6. multiproce ...