C-修改传递给函数的指针的地址 [英] C - modify the address of a pointer passed to a function
问题描述
我不明白为什么在此函数之外不保留对作为parameter
传递给function
的pointer
地址的修改(调用此函数后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屋!