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!请按任意键继续. . .
#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!请按任意键继续. . .
相关文章推荐
- char*数组(c类型字符串)和c++中的string的转换、字符串比较、char*数组等问题
- char[] char数组 char*返回值的问题
- python 调用C++,传递int,char,char*,数组和多维数组
- c++ char数组和char*
- {wchar_t*, wchar_t, wchat_t数组},{char,char*,char数组},{std::string,std::wstring},CString之间的相互转换
- char、char*、char**数组(有图,非常清楚)good
- char、char*、char**数组
- 【转】wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....相互之间的转化
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString转自由骑士大哥的)
- NSString与char*最大区别就是NSString是一个objective(父类)对象,而char*是一个有字节的数组。@+"字符串" 为objective-c NSString字符串常量的标准
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system("command")
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....转换
- char* 、 字符串字面值、char数组
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CStrin
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString....转换
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组
- char* ,char[],指针与数组
- char*数组(c类型字符串)和Vc++中的CString的转换,char*数组等问题
- char、char*、char** 数组
- std::string,std::wstring,CString,char,char*,char数组,wchar_t*,wchar_t,wchat_t数组