http://blog.csdn.net/u012627502/article/details/3579724
1)以返回值方式返回:把動態分配的存儲位置地址,賦值給指針類型返回值(不同于被調用函數的自動變量地址)
2)以形參形式返回:二級指針類型做形參
? 3?
? 4 void fun(int* p){
? 5 ? ? p = (int*)malloc(sizeof(int));
? 6 }
? 7?
? 8 int main(){
? 9 ? ? int* p_int = NULL;
?10 ? ? fun(p_int);
?11?
?12 ? ? printf("%d\n", *p_int);
?13 ? ? return 0;
?14 } //段錯誤
--------------------------------
? 1 #include <stdio.h>
? 2 #include <stdlib.h>
? 3?
? 4 int* fun(void){
? 5 ? ? return (int*)malloc(sizeof(int));
? 6 }
? 7?
? 8 int main(){
? 9 ? ? int* p_int = NULL;
?10 ? ? p_int = fun();
?11?
?12 ? ? *p_int = 12345;
?13?
?14 ? ? printf("%d\n", *p_int);
?15 ? ? free(p_int);
?16 ? ? return 0;
? 2 #include <stdlib.h>
? 3?
? 4 void fun(int** p){
? 5 ? ? *p = (int*)malloc(sizeof(int));
? 6 }
? 7?
? 8 int main(){
? 9 ? ? int* p_int = NULL;
?10 ? ? fun(&p_int);
?11 ? ? printf("%x\n", p_int);
?12 ? ? return 0;
??
? 1 #include <stdio.h>
? 2 #include <stdlib.h>? 3?
? 4 void fun(int* p){
? 5 ? ? p = (int*)malloc(sizeof(int));
? 6 }
? 7?
? 8 int main(){
? 9 ? ? int* p_int = NULL;
?10 ? ? fun(p_int);
?11?
?12 ? ? printf("%d\n", *p_int);
?13 ? ? return 0;
?14 } //段錯誤
--------------------------------
? 1 #include <stdio.h>
? 2 #include <stdlib.h>
? 3?
? 4 int* fun(void){
? 5 ? ? return (int*)malloc(sizeof(int));
? 6 }
? 7?
? 8 int main(){
? 9 ? ? int* p_int = NULL;
?10 ? ? p_int = fun();
?11?
?12 ? ? *p_int = 12345;
?13?
?14 ? ? printf("%d\n", *p_int);
?15 ? ? free(p_int);
?16 ? ? return 0;
?17 }//以返回值的方式返回動態申請的內存地址
?---------------------------------
? 1 #include <stdio.h>? 2 #include <stdlib.h>
? 3?
? 4 void fun(int** p){
? 5 ? ? *p = (int*)malloc(sizeof(int));
? 6 }
? 7?
? 8 int main(){
? 9 ? ? int* p_int = NULL;
?10 ? ? fun(&p_int);
?11 ? ? printf("%x\n", p_int);
?12 ? ? return 0;
?13 } //通過傳入參數二級指針返回
----------------------------------
不可以把局部變量的地址賦值給指針類型的返回值(生命周期決定的)
同類型結構體變量之間可以直接賦值。