动态内存可跨函数使用

 1 # include <stdio.h>
 2 
 3 void f(int **q)
 4 {
 5     int i =5;
 6             //*q = i;errro 因为*q = i;等价于 p = i;这样写是错的。
 7     //*q等价于p,q和**q都不等于p
 8 
 9      *q = &i;  //p = &i;
10 }
11 
12 int main()
13 {
14     int *p;
15 
16     f(&p);
17     printf("*p = %d\n",*p);//本语句语法上没有问题,但逻辑上有问题。想想!!能运行,编译器有问题
18                       //
19 
20     return 0;
21 
22 }
23 /*
24 在Vc++6.0中显示的结果是:
25 =============================================================
26 *p = 5
27 =============================================================
28 */
 1 # include <stdio.h>
 2 # include <malloc.h>
 3 void f(int ** q)
 4 {
 5     *q = (int *)malloc(sizeof(int));
 6     **q = 4;  // 理解: *q = p;   **q = *p可以赋值。
 7     
 8 
 9 
10 }
11 
12 int main(void)
13 {
14     int * p;
15     f(&p);
16     printf("*p = %d\n", *p);
17 
18     return 0;
19 
20 }
21 
22 /*
23 在Vc++6.0中显示的结果是:
24 =================================================
25 *p = 4
26 =================================================

27  */ 

posted on 2012-09-05 20:52  Your Song  阅读(189)  评论(0编辑  收藏  举报

导航