char aa[]="123456789123456789123456789";
char bb[4]={0};
1、strcpy(bb,aa); bb的空间,不能存下aa的内容,导致踩到aa的内存。如何解决这个问题?
2、使用strncpy,如下:
   strncpy(bb,aa,sizeof(bb)); 存在问题,bb的空间全部存储有效数据,没有预留\0的位置,strlen(bb)的长度是不确定的。如何解决这个问题?
3、使用strncpy(bb,aa,sizeof(bb)-1);
4、注意:strncpy(bb,aa,n); 表面意思是拷贝n个字节。实际上,不是这样。表示最多拷贝n个字节。如下:
   char aa[]="123456789123456789123456789";
   char bb[4]={0};
   strncpy(bb,aa,3); //拷贝3个字节,bb为 1,2,3,\0
   
   aa[1] = 0;
   strncpy(bb,aa,3); 按道理,应该拷贝3个字节,bb为1,\0,3,\0
   实际上,不是这样,而只是拷贝了1,\0, bb为1,\0,\0,\0
   也就是说,strncpy表示最多拷贝n个字节,来源的\0,会导致提前结束拷贝。

strcpy与strncpy的更多相关文章

  1. strcpy、strncpy与memcpy的区别与使用方法

    strcpy.strncpy.memcpy这三个C语言函数我们在主机代码编写中会很频繁的使用到,但是三个函数的区别.使用时该注意什么还是有必要说下的. 本文参考<C 标准库>编写. 一.函 ...

  2. strcpy、strncpy、memcpy的区别

    一.strcpy.strncpy区别 struct gpInfo { char gpcode[9]; char gpName[50]; }; string gpstr = "SZ000001 ...

  3. [置顶] strcpy()与strncpy()的区别

    头文件:#include <string.h> strcpy() 函数用来复制字符串,其原型为: char *strcpy(char *dest, const char *src); [参 ...

  4. C语言 - strcpy和strncpy的编程实现及总结

    一.字符串的strcpy与strncpy函数 1.编程实现strcpy函数(笔试很容易考到) 要求: 原型:char *stpcpy(char *strDest,char *strSrc); 头文件: ...

  5. c常用函数-strcpy和strncpy

    strcpy和strncpy strcpy在脚本开发中经常用到,例如处理参数等,它的作用是把一个字符串复制到另一个字符串中. strncpy的作用是把一个字符串中的指定长度复制到另一个字符串中,如果指 ...

  6. strcpy、strncpy 和安全的strncpy_s

    strcpy和strncpy摘于linux 内核源码的/lib/string.c char *self_strcpy(char *dest, const char *src) { char *tmp ...

  7. Linux C 字符串函数 strlen()、strcat()、strncat()、strcmp()、strncmp()、strcpy()、strncpy() 详解

      strlen(返回字符串长度) 表头文件 #include <string.h> 定义函数 size_t strlen(const char *s); 函数说明 strlen()用来计 ...

  8. C++ strcpy strcpy_s strncpy strlcpy

    strncpy的用法:它与strcpy的不同之处就在于复制n个字符,而不是把所有字符拷贝(包括结尾'\0'). 函数原型:char * strncpy(char *dst,const char * s ...

  9. c语言中几个常见的库函数strlen、strcmp、strcat、strcpy、strncpy、memset、memcpy、memmove、mmap

    1.strlen() 1)计算给定字符串的长度,不包括’\0’在内 unsigned int strlen(const char *s) { assert(NULL != s);//如果条件不满足,则 ...

  10. strcpy和strncpy

    在c语言中,对于简单变量,如int型.double型,直接使用赋值符号“=”,即可完成赋值,如 int a=10: int b: b=a: 即可完成用a给b赋值. 但是对于字符串,这样赋值是不准确的. ...

随机推荐

  1. Dfs【P2052】 [NOI2011]道路修建

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道 ...

  2. iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置

    iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置 ios9音频应用播放控制 在“iOS 9音频应用播放音频之ios9音频基本功能”一文可以看到AVAudioPlayer类有很多的属性以及方法 ...

  3. ubuntu下mysql的安装

    1.在终端输入 sudo apt-get install mysql-server mysql-client 2.在此安装过程中会让你输入root用户(管理MySQL数据库用户,非Linux系统用户) ...

  4. POJ2955【区间DP】

    题目链接[http://poj.org/problem?id=2955] 题意:[].()的匹配问题,问一个[]()串中匹配的字符数,匹配方式为[X],(X),X为一个串,问一个长度为N(N<= ...

  5. django-附件上传、media、extra、事务

    1 普通上传 1.1 html <form action="/index/" method="post" enctype="multipart/ ...

  6. Codeforces Beta Round #6 (Div. 2 Only) A. Triangle 水题

    A. Triangle 题目连接: http://codeforces.com/contest/6/problem/A Description Johnny has a younger sister ...

  7. FPGA LVDS I/O as an Analog Programmable Comparator

    http://www.eetimes.com/author.asp?section_id=36&doc_id=1320289 Seeing the new ADC IP being bandi ...

  8. vue项目条形码和二维码生成工具试用

    项目开发需要,优惠券分不同类型,简单的使用id生成条形码供店铺使用,麻烦点的需要多个字段的就需要使用二维码来展示了,对应的效果如下 条形码(一维码)使用工具code128 需引入code128.js ...

  9. C#各种结束进程的方法详细介绍

    Process类的CloseMainWindow, Kill, Close Process.CloseMainWindow是GUI程序的最友好结束方式,从名字上就可以看出来它是通过结束主窗体,相当于用 ...

  10. Spark Client和Cluster两种运行模式的工作流程

    1.client mode: In client mode, the driver is launched in the same process as the client that submits ...