void test3(char * str1) {
if(str1==NULL) {
return;
}
char string[10];
if(strlen(str1)<=10) {
strcpy(string,str1);
} }

 

分析:因为strlen的结果未统计'\0'所占用的1个字节。只有把程序中的<=改为<,这样可以保证str1最末尾的字符是\0,因为strcpy函数是从源地址开始拷贝,直到遇到\0为止。

注意sizeof会统计最后的\0,strlen不会。

3.strcpy使用注意(3)的更多相关文章

  1. strcpy函数在VS2015无法使用的问题

    一:原因:一般认为是vs准备弃用strcpy的,安全性较低,所以微软提供了strcpy_s来代替 然而,strcpy_s并没有strcpy好用,我们要想继续在VS2015中使用strcpy该怎么办 呢 ...

  2. 你必须知道的指针基础-4.sizeof计算数组长度与strcpy的安全性问题

    一.使用sizeof计算数组长度 1.1 sizeof的基本使用 如果在作用域内,变量以数组形式声明,则可以使用sizeof求数组大小,下面一段代码展示了如何使用sizeof: ,,,,,}; int ...

  3. strcpy 函数的实现

    原型声明:extern char *strcpy(char *dest,const char *src); 头文件:string.h   功能:把从src地址开始且含有‘\0’结束符的字符串赋值到以d ...

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

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

  5. strncpy,strcpy

    strncpy不会为des自动添加“\0” strcpy遇空结束,自动添加结束符 结论: 1.使用strcpy时一定不能用于无结束符的字符串,因为strcpy依赖\0判断源字符串的结束 2.使用str ...

  6. strcpy strlen memcpy等的函数实现

    #include <assert.h> #include <string.h> #include <stdlib.h> #include <stdio.h&g ...

  7. 字符串strcpy

    strcpy函数的表达方式: //把一个char组成的字符串循环右移n个,如:“abcdefghi",n=2,移动后"hiabcdefgh" #include <i ...

  8. strlen(); strcpy(); strcat(); strcmp() ---笔记

    指针小知识点: int a =10; int *p=&a; int *q=p;        //p中保存的是a的地址 int *q=p;       //将p的值赋给q 作用是让q也指向a ...

  9. strcpy 库函数 拷贝函数

    strcpy 是在string.h 里面 #include "stdafx.h"#include "string.h"struct Student{int Se ...

  10. strcpy和memcpy的区别(转载)

    strcpy和memcpy都是标准C库函数,它们有下面的特点.strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符. 已知strcpy函 ...

随机推荐

  1. C#中的编译开关

    c#不支持宏定义,只支持编译开关. ======================================我想加上#define xxx就编译a代码,否则编译b代码,像这样的:#if xxxa ...

  2. BZOJ1355: [Baltic2009]Radio Transmission KMP

    Description 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. Input 第一行给出字符串的长度,1 < L ≤ 1, ...

  3. BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟

    Description Recently, the cows have been competing with strings of balanced  parentheses and compari ...

  4. Facebook广告API系列 2 - Audience Management

    Facebook广告API系列 2 Facebook marketing API有三大组成部分: Audience Management Ads Management Ads Insights 本篇稍 ...

  5. javascript的一些在IE下不支持的函数小结

    // ============ isArray ===============// // isArray function isArray(value){ return Object.prototyp ...

  6. 【转】Makefile 中:= ?= += =的区别

    最近接触使用C++项目,需要使用Makefile,因此需要好好学习下. [转自]:http://www.cnblogs.com/wanqieddy/archive/2011/09/21/2184257 ...

  7. 05_Flume_timestamp interceptor实践

    1.目标场景 2.Flume Agent配置 # specify agent,source,sink,channel a1.sources = r1 a1.sinks = k1 a1.channels ...

  8. python ros 使用launch文件启动脚本

    目录结构 在包里面新建scripts文件夹,里面放运行的脚本文件,记得设置执行权限 然后新建launch文件夹,新建launch文件按照如下格式写: <node pkg="initia ...

  9. Jmeter Connect to Cassandra

    https://github.com/Netflix/CassJMeter/wiki https://stackoverflow.com/questions/40974407/connecting-c ...

  10. MAC OS 英语朗读功能

    哈哈哈,太神奇了 在命令行中敲say + word ,系统能够自己讲word读出来. 如果是敲的是 say +中文, 就不知道再读什么啦 哈哈哈哈---- 此外,在对应网站选中内容后还可以右击,用sp ...