您的位置:首页 > 其它

char数组和char*还有strcpy函数

2016-08-12 01:25 197 查看
#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!请按任意键继续. . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐