1 /* 函数传递指针是传递的指针指向的地址,不是指针本身;
2 * 如何想操作指针本身,需要使用指向指针的指针;
3 */
4
5 #include <stdio.h>
6
7 #include <stdlib.h>
8
9 void AssignSpace(int ** avr ,int size ,int data)
10 { int i;
11 *avr = (int *)malloc(size * sizeof(int));
12
13 for(i = 0; i<size;i++)
14 {
15 *(*avr+i) = data;
16 }
17 }
18
19 void AssignSpace_pointer(int *avr , int size ,int data)
20 {
21 int i;
22
23 avr = (int*)malloc(size * sizeof(int));
24
25 for(i=0;i<size;i++)
26 {
27 *(avr+i) = data;
28 }
29
30 for(i=0;i<size;i++)
31 printf("the inner pointer avr[%d] is : %x \n",i,*avr);
32 }
33
34 void main()
35 {
36 int* hello = NULL;
37 int* hello_pointer = NULL;
38 int i;
39
40 AssignSpace(&hello ,5 ,0x24);
41 AssignSpace_pointer(hello_pointer, 5 ,0x35);
42
43 for(i=0;i<5;i++)
44 printf("the hello data hello[%d] is : %x \n",i,*(hello+i));
45
46 if(hello_pointer == NULL)
47 {
48 printf("the hello_pointer is NULL\n");
49 }
50 else{
51
52 for(i=0;i<5;i++)
53 printf("the hello data hello_pointer[%d] is : %x \n",i,*(hello_pointer+i));
54 }
55
56 }
运行结果:
![容易忽略 -- 函数传递的指针是数据的地址,不是指针本身 容易忽略 -- 函数传递的指针是数据的地址,不是指针本身](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzU5Ny9iZDhkN2E0MjNlZTAzMGMyODQxMWFiNzA0NDRmNjA2NS5wbmc=)
分析:
AssignSpace_pointer(int *avr , int size ,int data);函数中avr指针只是复制hello_pointer指向的地址;当在函数中操作avr时,只是操作局部变量avr,
并不会改变hello_pointer指针;