一个关于指针与双重指针区别的小实验

 
1
#include<iostream>
2 using namespace std;
3 void test1(int n,char* ptr)
4 {
5 cout<<"the address of ptr:"<<&ptr<<endl;
6 cout<<"this is the test in test1:"<<ptr<<endl;
7 ptr="mk";
8 }
9 void test2(int n,char** ptr)
10 {
11 cout<<"this is the test in test2:"<<*ptr<<endl;
12 *ptr="mk2";
13 }
14 int main(void)
15 {
16 char *pps="this sss";
17
18 cout<<"this is original string:"<<*pps<<endl;
19 test1(1,pps);
20 cout<<"this is the string after test1:"<<pps<<endl;
21 char**ptr=NULL;
22 ptr=&pps;
23 test2(1,ptr);
24 cout<<"this is the string after test2:"<<pps<<endl;
25 return 1;
26 }

定义为char*的变量不能再参数为char*的函数中对其进行修改,但却可以在以双重指针为参数的函数中进行修改,如上面的例子所示。

运行结果:

 1 #include<iostream>
2 using namespace std;
3 void int_swap(int *a,int *b)
4 {
5 int temp;
6 temp=*a;
7 *a=*b;
8 *b=temp;
9 }
10 int main()
11 {
12
13 int m=10;
14 int n=0;
15 cout<<"m="<<m<<",n="<<n<<endl;
16 int_swap(&m,&n);
17 cout<<"m="<<m<<",n="<<n<<endl;
18 return 1;
19 }

运行结果如下:

其实上面这个小实验没什么用处,本来是要说明int*无法在函数中直接修改来着,但编出来才发现用int*时是可以被修改的。

后来才发现原来不能被直接修改的只有char*,再后来,发现函数的char*参数是可以被修改的,但是对传入的数据类型有特殊的要求。看下面则个例子:

 1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4
5 char* trim(char *s);
6 char* leftstring(char *s1, char *s2, int n);
7 int index(char *s1, char *s2);
8 /////////////////
9 int main(){
10 char str1[]="I'm student. ",str2[]="student",str3[4]; //定义为这种形式时时可以直接被修改的
11 /*
12 char *str1="I'm student. "; //这里是重点,定义为被注释的这种形式
13 char *str2="student"; //时是不可以在leftstring函数中被修改的
14 char str3[4];
15 */
16 int n;
17 cout<<"串str1:"<<str1<<"长度为:"<<strlen(str1)<<endl;
18 trim(str1);
19 cout<<"串str1:"<<str1<<"长度为:"<<strlen(str1)<<endl;
20 leftstring(str1,str3,3);
21 cout<<"串str3:"<<str3<<"长度为:"<<strlen(str3)<<endl;
22 cout<<"串str2是:"<<str2<<endl;
23 n=index(str1,str2);
24 if(n!=-1) cout<<"串str1包含子串str2,从第"<<n<<"字符开始(由0开始计数)。"<<endl;
25 else cout<<"串str1不包含子串str2"<<endl;
26 return 0;
27 }
28 /////////////////
29 char* trim(char *s){
30 int i=0;
31 while(*(s+i)!='\0') i++;
32 i--;
33 while(*(s+i)==' ') *(s+i--)='\0';
34 return s;
35 }
36 ///////////////////
37 char* leftstring(char *s1, char *s2, int n){
38 int i;
39 for(i=0;i<n;i++) *s2++=*s1++;
40 *s2='\0';
41 return s2;
42 }
43 //////////////////
44 int index(char *s1, char *s2){
45 bool b=0;
46 int i,j,n=-1,n1=strlen(s1),n2=strlen(s2);
47 for(i=0;i<n1;i++){
48 if(*(s1+i)==*s2){
49 b=1;
50 for(j=0;j<n2;j++){
51 if(*(s1+i+j)!=*(s2+j)){
52 b=0;
53 break;
54 }
55 }
56 if(b==1) {n=i;break;}
57 }
58 }
59 return n;
60 }
61 /////////////////

上面程序中那段注释是要说明的问题,具体原因没有找到,高手知道的话谢谢解答!

个人感觉比较权威的两篇博客文章如下:

http://blog.csdn.net/feiyinzilgd/article/details/5302369

http://www.cnblogs.com/harlentan/archive/2011/01/14/2006450.html

上述文章对二维指针与一维指针的理解是很有帮助的,但是对char*数据进行操作时仍然有问题。很多书籍似乎都避重就轻,并没有指出这种情况下该如何操作。目前已知的更改char*数组的方法只有char**(二维指针)这一种方法。(在传入数据类型有char*变为char[]时会不同)

另外通过如上两篇文章页可以看出,当输入参数为int*时并不需要通过二维指针进行更改,二维指针和一维指针可以完成相同的功能。似乎二维指针专为char*这种情况而设。

 

如下内容转自新浪博客:http://blog.sina.com.cn/s/blog_77e5ef530100sceg.html

主要讲了通过双重指针修改数据和指针解引用的知识。因为上面考虑的都是调用函数对指针的修改的问题,所以这里值引用和上面相关的程序如下:

 1 #include <stdio.h>
2
3 void find(char arr[], char search, char** pa)
4
5 {
6
7 int i;
8
9 for (i=0;*(arr+i)!=0;i++)
10
11 {
12
13 if (*(arr+i)==search)
14
15 {
16
17 *pa=(arr+i);
18
19 break;
20
21 }
22
23 else if (*(arr+i)==0)
24
25 {
26
27 *pa=0;
28
29 break;
30
31 }
32
33 }
34
35 }
36
37 void main()
38
39 {
40
41 char str[]={"afsdfsdfdf\0"};
42
43 char a='d';
44
45 char *p; //存放所找到的字符在字符串中所在的地址
46
47 char **pp1=0;
48
49 pp1=&p;//将指针p在内存中所在的地址赋给pp1。
50
51 find(str,a,pp1);
52
53 if (0==p )
54
55 {
56
57 printf ("没找到!\n");
58
59 }
60
61 else
62
63 {
64
65 printf("在字符串中第四个字符也就是d在内存中的地址 0x%x \n",str+3);
66
67 printf("找到了,p=0x%x",p);
68
69 }
70
71 getchar();
72
73 }
另外还有一个strcpy的程序:
 1 char* strcyp(char *desChar,const char *srcChar)
2 {
3 char *desCharCopy=desChar;
4 if((desChar==NULL)||(srcChar==NULL))
5 {
6 cout<<"the array you put in is wrong!"<<endl;
7 exit(1);
8 }
9 while((*desChar++=*srcChar++)!='\0');
10 return desChar;
11 }
12 int main(void)
13 {
14 char des[]="abcde";
15 char src[]="qwertty";
16 //字符串可以定义成如上形式,不可以定义成如下形式,否则strcyp运行不通过
17 //char *des="abcde";
18 //char *src="qwertty";
19 strcyp(des,src);
20 cout<<"des:"<<des<<endl;
21 cout<<"src:"<<src<<endl;
22 return 1;
23 }


posted on 2012-03-14 10:39  专吃兔子的大黑猫  阅读(1088)  评论(0编辑  收藏  举报