C申請內存函數


#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;

}
發佈了125 篇原創文章 · 獲贊 31 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章