9-malloc練習

malloc 練習 :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void getmemory(char *p)
{
    p = (char *)malloc(100);
}
int main(int argc, const char *argv[])
{
    char *str = NULL;
    getmemory(str);  //  注意 : 此時函數傳參 的爲 變量str的值  .
    strcpy(str,"hello world");
    printf("%s\n",str);
    free(str);
    return 0;
}
發佈了38 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章