2.strcpy使用注意(2)

分析下述代码:

void test2() {
	char string[10],str1[10];
	int i;
	for(i=0;i<10;i++) {
		srtr1=‘a‘;
	}
	strcpy(string,str1);
}

  代码不能通过编译。str1为char *const类型的右值类型,根本不能赋值。若想对数组的第一个元素赋值,需要使用*str=‘a’;对字符数组赋值后,使用库函数strcpy进行拷贝操作,strcpy会从源地址一直往后拷贝,直到遇到‘\0’为止。所以拷贝长度是不定的。如果一直没有遇到‘\0’导致越界访问非法内存,程序就崩了。

代码修改如下:

void test2() {
	char string[10],str1[10];
	int i;
	for(i=0;i<9;i++) {
		srtr1[i]=‘a‘;
	}
	str1[9]=‘\0‘
	strcpy(string,str1);
}

  

时间: 2024-08-02 16:37:12

2.strcpy使用注意(2)的相关文章

strcpy和strcat易忽略点

首先来看一段C程序: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 void GetMem(char*& pstr){//注意必须要用指针的指针或者指针的引用.如果传本身,返回的已经是空悬指针了 6 pstr=(char*)malloc(20); 7 } 8 9 int main(){ 10 char* str; 11 GetMem(str); 12 13 strcpy(

warning C4996: &#39;strcpy&#39;: This function or variable may be unsafe. Consider using strcpy_s instead.

使用VS2005以上版本(VS2005.VS2008.VS2010)编译在其他编译器下正常通过的C语言程序,你可能会遇到类似如下的警告提示: 引用内容warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for det

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

指针小知识点: int a =10; int *p=&a; int *q=p;        //p中保存的是a的地址 int *q=p;       //将p的值赋给q 作用是让q也指向a strlen( ); 求字符串的长度 strcpy( ); 复制字符串 strcat( ); 连接字符串 strcmp( ); 字符串大小的比较 1 typedef unsigned int size_t 2 3 size_t my_strlen (const char *str) // strlen()

字符串拷贝函数strcpy写法_转

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// CppReference.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" using namespace std; /* * 说明:字符串拷贝版本1 * 参数:dest目标地址,src源地址 * 返回:返回拷贝好的地址:如果出错或者有重叠,无

虚怀若谷 字符串拷贝函数strcpy写法_转

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// CppReference.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" using namespace std; /* * 说明:字符串拷贝版本1 * 参数:dest目标地址,src源地址 * 返回:返回拷贝好的地址:如果出错或者有重叠,无

strcpy自实现

为了避免strcpy源串覆盖问题(P220),自实现strcpy. #include <stdio.h> #include <string.h> #include <assert.h> #include <malloc.h> void myStrcpy(char *to, char *from) { assert(to != NULL && from != NULL); while(*from != '\0'){ *to ++ = *from

strcpy和memcpy的区别

strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符. 已知strcpy函数的原型是:char* strcpy(char* dest, const char* src);memcpy提供了一般内存的复制.即memcpy对于需要复制的内容没有限制,因此用途更广. char * strcpy(char * dest, const char * src) // 实现src到dest的复制 { if ((src == NULL) || (d

strcat与strcpy

前几天刚有点认识了strlen和sizeof,今天又有点认识了strcpy和strcat. 在vc2010环境下 1 strcpy strcpy(p,q),p要么为数组,要么为空指针,要是为空指针操作后内存中存储的形式和数组一样,空处为\0,非空指针(分配了内存)报错. 2 strcat strcat(p.q),p要么为数组,要么为strcpy后产生的指针,若为指针(申请了内存)非空报错,若空 (申请了内存)不报错但存在部分乱码. 网上对此有各种说法,但为了程序的可维护性和移植性,第一个参数尽量

strcpy, memcpy, memset函数

一. strcpy函数 原型声明:char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 和 #include <stdio.h> 功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间 说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串. 返回指向dest的指针. 实现代码: char * strcpy(char * strDest,c

strcpy函数

功能:char *strcpy(char *str1,char *str2),把字符串2复制到字符串1. 源码: char *strcpy(char *str1,char *str2) { assert(str1 != NULL && str2 != NULL); char *p = str1; while( *str1++ == *str2++); return p; } 在练习题中的实现,编译环境vs2008: 1 /*Test*/ 2 #include<stdio.h>