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

发布时间:2022-03-25 09:30:19 作者:iii
来源:亿速云 阅读:160

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

先看两个程序:

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++指针参数传递实质及二级指针怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. c#中一级指针,二级指针,指针数组,数组和指针的示例分析
  2. c 二级指针3种内存分配

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:如何理解c++右值引用和移动构造

下一篇:c++结构体有默认的构造函数吗

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》