C語言傳指針類型的形參

今天在牛客網上做C語言專項練習題,遇到一個“函數傳指針類型的形參”的題,我做錯了,正確的爲下面代碼:

#include   <string.h>
#include   <stdio.h>
#include   <stdlib.h>

void getmemory(char** p)  {
    *p=(char *) malloc(100);
    strcpy(*p,"hello world");
}

int main( )
{
    char *str=NULL;
    getmemory(&str);
    printf("%s\n",str);
    free(str);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章