C-修改传递给函数的指针的地址 [英] C - modify the address of a pointer passed to a function

查看:88
本文介绍了C-修改传递给函数的指针的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么在此函数之外不保留对作为parameter传递给functionpointer地址的修改(调用此函数后ptr的地址不会更改) ):

I don't understand why the modification of pointer address passed as parameter to a function isn't persisted outside of this function (the address of ptr doesn't change after this function is called):

void advance(int *ptr) {
    ptr = ptr + 1
}  

当我可以在同一函数内修改ptr指向的value:*ptr = *ptr + 1.

When I can inside this same function modify the value pointed by ptr: *ptr = *ptr + 1.

PS:我知道我可以使用pointer to a pointer:**ptr.

PS: I know that I can achieve what I want using a pointer to a pointer: **ptr.

推荐答案

因为C不是不是通过引用调用,所以它始终是按值调用,即使引用/指针为参数.

Because C is not call by reference, it is always call-by-value, even with references/pointers as arguments.

与其他语言不同,它可以区分参数类型.

It is not like other languages, where it can differentiate between argument types.

这篇关于C-修改传递给函数的指针的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆