C pointer malloc 問題 ........

The below have problem:
  1. int main(){
  2.     struct term *a;
  3.     to_malloc(a);
  4. }

  5. void to_malloc(term *a){
  6.     a=malloc(10*sizeof(term));
  7. }
複製代碼
The below is ok:
  1. int main(){
  2.     struct term *a;
  3.     a=malloc(10*sizeof(term));
  4. }
複製代碼
can anyone expplain it ?  thx so much

you must not use "pass by value"
pass by ref plx
  1.     int main(){
  2.         struct term *a;
  3.         to_malloc(&a);
  4.     }

  5.     void to_malloc(term **a){
  6.         *a=malloc(10*sizeof(term));
  7.     }
複製代碼

TOP