# Include <iostream>
Using namespace STD;
// Apply memories in a function.
Void getmemory (char ** P)
{
* P = (char *) malloc (10 );
}
/**
* P will be free after getmemory2 invoked.
*/
Char * getmemory2 (void)
{
Char P [] = "Hello World ";
Return P;
}
/**
* Write a function which Concat a string to another string without using system library
*/
Char * strconcat (char * deststr, const char * srcstr)
{
Char * Header = deststr;
While (* deststr! = '/0 ')
{
Deststr ++;
}
While (* srcstr! = '/0 ')
{
* Deststr ++ = * srcstr ++;
}
* Deststr = '/0 ';
Return header;
}
Int main ()
{
// 1 ./*
Char * P = NULL;
Getmemory (& P );
Strcpy (P, "hello ");
Cout <p <Endl;
Free (P );
// 2.
Char * P2 = NULL;
P2 = getmemory2 ();
Strcpy (P2, "hello ");
Cout <P2 <Endl;
// 3.
Char * STR = (char *) malloc (sizeof (100 ));
Free (STR );
If (STR! = NULL)
{
Strcpy (STR, "world ");
Cout <STR <Endl;
}
// 4.
Char str1 [15] = "hello ";
Char * str2 = "world ";
Strconcat (str1, str2 );
Cout <str1 <Endl;
Return 0;
}