#include <iostream>
#include <string>
using namespace std;
//利用指针传值
char *TestPointerForwardValue(char *pChar2)
{
char *pChar=new char[strlen(pChar2)+1];
if(NULL==pChar)
{
exit(0);
}
strcpy(pChar,pChar2);
return pChar;
}
//利用指针的引用传值
char *TestReferencePointer(char *&rpChar)
{
char *pChar=new char[strlen(rpChar)+1];
if(NULL==pChar)
{
exit(0);
}
strcpy(pChar,rpChar);
cout<<pChar<<endl;
return pChar;
}
char *TestReturnDynamicMemory(char a[])
{
char b[100];
char *pConst="Sichuan University!";
char *pChar=new char[strlen(a)+strlen(pConst)+1];
if(NULL==pChar)
{
exit(0);
}
strcpy(b,a);
cout<<b<<endl;
strcpy(pChar,a);
strcat(pChar,pConst);
return pChar;
}
char *TestReturnStaticMemory()
{
char *p="Diaoyu islands is Chinese inherent territory.";
cout<<p<<endl;
return p;
}
void main()
{
char al[]="Hello,Sichuan University!";
char *pl=al;
char a2[]="Hello,College of Computer!";
char *p2=a2;
char *p=NULL;
cout<<sizeof(al)<<";"<<strlen(pl)<<endl;
p=TestPointerForwardValue(pl);
cout<<p<<endl;
delete []p;
p=TestReferencePointer(p2);
cout<<p<<endl;
delete []p;
p=TestReturnDynamicMemory(a2);
cout<<p<<endl;
delete []p;
p=TestReturnStaticMemory();
cout<<p<<endl;
cin.get();
}
标准类型和类类型之间的类型转化8_3
最新推荐文章于 2020-08-19 11:29:40 发布