再说指针

最近在做一个函数实现,用到通过函数形参来返回有效数据,用到指针,特意翻了翻《C专家编程》,发现以前看过的很多知识点都忘了。认真分析了一下,得出了一些自己的理解,再次记录一下,以便以后查阅

 #include <stdio.h> void SetDate(int *pndata,int ncounter) { int i; int *pnData; pnData = pndata; printf("pnData value is %p/n pnData addr is %p/n",pnData,&pnData); for(i = 0; i < ncounter; i++) { *pnData = 0; pnData++; } } int main(void) { int nPreData[10] = {1,2,3,4,5,6,7,8,9,0}; int i; for(i = 0; i < 10; i++) { printf("nPreData[%d] = %d/n",i,nPreData[i]); } printf("nPreData addr is %p/n",nPreData); printf("nPreData[0] addr is %p/n",&nPreData[0]); SetDate(nPreData,10); for(i = 0; i < 10; i++) { printf("nPreData[%d] = %d/n",i,nPreData[i]); } return 0; }

 运行结果:

nPreData[0] = 1

..

nPreData[9] = 0

nPreData addr is 0012FF3C

nPreData[0] addr is 0012FF3C

pnData value is 0012FF3C

pnData addr is 0012FE3C

nPreData[0] = 0

..

nPreData[9] = 0

 

发现数组数据改变。

 

#include <stdio.h> void SetDate(int *pndata,int ncounter) { int i; static int nMemData[10]; int *pnData; pnData = nMemData; pndata = pnData; printf("pnData value is %p/n pnData addr is %p/n",pnData,&pnData); for(i = 0; i < ncounter; i++) { *pnData = 0; pnData++; } } int main(void) { int nPreData[10] = {1,2,3,4,5,6,7,8,9,0}; int i; for(i = 0; i < 10; i++) { printf("nPreData[%d] = %d/n",i,nPreData[i]); } printf("nPreData addr is %p/n",nPreData); printf("nPreData[0] addr is %p/n",&nPreData[0]); SetDate(nPreData,10); for(i = 0; i < 10; i++) { printf("nPreData[%d] = %d/n",i,nPreData[i]); } return 0; }

 运行结果:

 

nPreData[0] = 1

..

nPreData[9] = 0

nPreData addr is 0012FF3C

nPreData[0] addr is 0012FF3C

pnData value is 00417140

pnData addr is 0012FE3C

nPreData[0] = 1

..

nPreData[9] = 0

 

 发现数组数据没有改变。

#include <stdio.h> void SetDate(int **pndata,int ncounter) { int i; static int nMemData[10]; int *pnData; pnData = nMemData; *pndata = pnData; printf("pnData value is %p/n pnData addr is %p/n",pnData,&pnData); for(i = 0; i < ncounter; i++) { *pnData = 0; pnData++; } } int main(void) { int nPreData[10] = {1,2,3,4,5,6,7,8,9,0}; int *pnPreData; int i; for(i = 0; i < 10; i++) { printf("nPreData[%d] = %d/n",i,nPreData[i]); } printf("nPreData addr is %p/n",nPreData); //printf("nPreData[0] addr is %p/n",&nPreData[0]); pnPreData = nPreData; SetDate(&pnPreData,10); printf("pnPreData value is %p/n pnPreData addr is %p/n",pnPreData,&pnPreData); //printf("nPreData addr is %p/n",nPreData); //printf("nPreData[0] addr is %p/n",&nPreData[0]); for(i = 0; i < 10; i++) { printf("nPreData[%d] = %d/n",i,nPreData[i]); } return 0; }

 运行结果:

 

nPreData[0] = 1

..

nPreData[9] = 0

nPreData addr is 0012FF3C

pnData value is 00417140

pnData addr is 0012FE3C

pnPreData value is 00417140

pnData addr is 0012FF30

nPreData[0] = 1

..

nPreData[9] = 0

指向数组的指针改变了 但数组数据并没有改变。

 #include <stdio.h> void SetDate(int **pndata,int ncounter) { int i; int *pnData; pnData = (*pndata); printf("*pndata value is %p/n pndata addr is %p/n",*pndata,pndata); for(i = 0; i < ncounter; i++) { *pnData = 0; pnData++; } } int main(void) { int nPreData[10] = {1,2,3,4,5,6,7,8,9,0}; int *pnPreData; int i; for(i = 0; i < 10; i++) { printf("nPreData[%d] = %d/n",i,nPreData[i]); } printf("nPreData addr is %p/n",nPreData); pnPreData = nPreData; SetDate(&pnPreData,10); printf("pnPreData value is %p/n pnPreData addr is %p/n",pnPreData,&pnPreData); for(i = 0; i < 10; i++) { printf("nPreData[%d] = %d/n",i,nPreData[i]); } return 0; }

 

 运行结果:

nPreData[0] = 1

..

nPreData[9] = 0

nPreData addr is 0012FF3C

*pnData addr is 0012FF3C

pnData addr is 0012FF30

pnPreData value is 0012FF3C

pnData addr is 0012FF30

nPreData[0] = 0

..

nPreData[9] = 0

数组数据改变了,但指向他们的指针并没有改变。

 

结论:

对于传入指针变量情况,并要通过函数参数来返回有效数据的函数,函数参数需要声明为指针的指针形式,才能改变外部传入的指针变量的值或者指针变量指向的内容。

 对于传入的对象非指针变量的情况,函数只需声明为指针变量形式即可。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(编程,c)