一、指针
一级指针:是一个指针变量,指向一个普通变量,并保存该普通变量的地址;
二级指针:是一个指针变量,指向一个一级指针,并保存该一级指针的地址;
二、一级指针的引用
2.1一级指针传参,改变不了原指针的地址,尽管t++,改变了地址,一旦此函数运行结果,就会被释放,不会影响到main函数的地址。
#include <stdio.h>
int array[4]={1,2,3,4};
void test(int *t)
{
t++;
printf("t=%p\n",t);
;
}
int main()
{
int *p=array;
printf("p=%p\n",p);
test(p);
printf("p=%p\n",p);
printf("%x %d %d %d\n",array[0],array[1],array[2],array[3]);
}
运行结果
p=0x404030
t=0x404034
p=0x404030
1 2 3 4
那么一级指针传参,用来做什么?
显而易见,可以改变该指针指向的内容,如下程序,把数组的里面的1改为5.
#include <stdio.h>
int array[4]={1,2,3,4};
void test(int *t)
{
*t = 5;
//t=0x114455;
}
int main()
{
int *p=array;
printf("p=%p\n",p);
test(p);
printf("p=%p\n",p);
printf("%d %d %d %d\n",array[0],array[1],array[2],array[3]);
}
运行结果
p=0x404030
p=0x404030
5 2 3 4
三、二级指针的引用
2.1二级指针指向一个一级指针,再通此一级指针指向该地址。
1)用处:二级指针可以改变一级指针的地址,也可以改变一级指针指向的内容。
#include <stdio.h>
int array[4]={1,2,3,4};
void test(int **t)
{
//*t = 0x114455;
**t =5;
}
int main()
{
int *p=array;
printf("p=%p\n",p);
test(&p);
printf("p=%p\n",p);
printf("%d %d %d %d\n",array[0],array[1],array[2],array[3]);
}
运行结果
p=0x404030
p=0x404030
5 2 3 4
#include <stdio.h>
int array[4]={1,2,3,4};
void test(int **t)
{
*t = 0x114455;
}
int main()
{
int *p=array;
printf("p=%p\n",p);
test(&p);
printf("p=%p\n",p);
printf("%d %d %d %d\n",array[0],array[1],array[2],array[3]);
}
运行结果
p=0x404030
p=0x114455
1 2 3 4
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)