资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c++指针参数传递实质及二级指针怎么使用

这篇文章主要介绍了c++指针参数传递实质及二级指针怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇c++指针参数传递实质及二级指针怎么使用文章都会有所收获,下面我们一起来看看吧。

成都创新互联是一家专业提供偃师企业网站建设,专注与网站设计、网站制作html5、小程序制作等业务。10年已为偃师众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

先看两个程序:

1:

void test(char *p)

{

       printf("[test1][p]:%p.\n",p);

       printf("[test2][p]:%s.\n",p);

       p=(char *)malloc(10);

       strcpy(p,"ABCDE");

       printf("[test3]malloc之后.....\n");

       printf("[test4][p]:%p.\n",p);

       printf("[test5][p]:%s.\n",p);

       free(p);

}

int main()

{

       char b[6] = "abcde";

       char *a = b;

       printf("[main1][a]:%p.\n",a);

       printf("[main2][a]:%s.\n",a);

       test(a);

       printf("[main3][a]:%p.\n",a);

       printf("[main4][a]:%s.\n",a);

       return 0;

}

输出结果:                         注意:(test函数的pde值已改变,main函数的a的值未改变)

main1][a]:0xbfeaaef6.
[main2][a]:abcde.
[test1][p]:0xbfeaaef6.
[test2][p]:abcde.
[test3]malloc之后.....
[test4][p]:0x8a52008.
[test5][p]:ABCDE.
[main3][a]:0xbfeaaef6.
[main4][a]:abcde.

2:

void test(char **p)

{

       printf("[test1][p]:%p.\n",p);

       printf("[test2][*p]:%p.\n",*p);

       *p=(char *)malloc(10);

       strcpy(*p,"ABCDE");

       printf("[test3]malloc之后.....\n");

       printf("[test4][p]:%p.\n",p);

       printf("[test5][*p]:%p.\n",*p);

       printf("[test6][*p]:%s.\n",*p);

       free(*p);

}

int main()

{

       char b[6] = "abcde";

       char *a = b;

       printf("[main1][a]:%p.\n",a);

       printf("[main2][a]:%s.\n",a);

       test(&a);

       printf("[main3][a]:%p.\n",a);

       printf("[main4][a]:%s.\n",a);

       return 0;

}

输出结果:                         注意:(test函数的pde值已改变,main函数的a的值也已经改变)

[main1][a]:0xbfaca776.
[main2][a]:abcde.
[test1][p]:0xbfaca770.
[test2][*p]:0xbfaca776.
[test3]malloc之后.....
[test4][p]:0xbfaca770.
[test5][*p]:0x9132008.
[test6][*p]:ABCDE.
[main3][a]:0x9132008.
[main4][a]:ABCDE.

关于“c++指针参数传递实质及二级指针怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“c++指针参数传递实质及二级指针怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。


当前题目:c++指针参数传递实质及二级指针怎么使用
网页链接:http://cdkjz.cn/article/gsodje.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220