C申请内存函数


#include 
using namespace std;

//传值调用
void GetMemory( char **p )
{
    *p = (char *) malloc( 100 );
}
//引用调用
void GetMemory_1(char *&p)
{
    p = (char *) malloc (100);
}

int main()
{
    char *str = NULL;
    char *str1 = NULL;
    GetMemory( &str );
    GetMemory_1( str1 );
    strcpy( str, "hello world" );
    strcpy( str1, "hello world1" );
    cout<cout<free(str);
    free(str1);
    return 0;

}

你可能感兴趣的:(C++)