C申请内存函数

来源:互联网 发布:k歌软件 编辑:程序博客网 时间:2024/04/30 05:22
#include <iostream>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<<str<<endl;    cout<<str1<<endl;    free(str);    free(str1);    return 0;}
0 0