char数组和char*还有strcpy函数

来源:互联网 发布:windows kill进程命令 编辑:程序博客网 时间:2024/05/18 06:19
#include<iostream>
#include<string.h>
#include<assert.h>


using namespace std;
char* Strcpy(char* des, const char* source)//址传递
{
char* r = des;

assert((des != NULL) && (source != NULL));

//首先必须判断两个指针是否为空,由于复制后的指针需要返回,因此需要一个指针来记录地址的初始值,最后将复制的结果返回是为了进行链式操作。 

while ((*des++ = *source++) != '\0');
return r;
}
int main()
{
char str[13];//ok
//char* str = new char[20];//ok
//char* str = (char*)malloc(20);//ok
//char* str;//errp 未初始化
 //char* str=NULL;//error 断言 null区不可改动
//char* str = " ";//error
//char* str = "0";//error
//char* str = "abcdefghluuuuuuu";//常量字符串常量区不可改动
//char* str = new char[1];//长度不够 error
//char* str = new char();//error应该是字符串 所以是字符数组




char* pstr = "hello world!";
Strcpy(str, pstr);
cout << strlen(Strcpy(str, pstr)) << endl;
cout<<sizeof(Strcpy(str, pstr))<<endl;
printf(str);


12
4
hello world!请按任意键继续. . .






1 0
原创粉丝点击