为什么要使用&用*传递函数的值? [英] Why use & to pass value for function with *?
问题描述
I wanted to ask the author, caf , from Understanding functions and pointers in C
我意识到我无法在同一页面上提问.
I realized I can't ask the question on that same page.
我仍然对使用&将指针的值传递给函数.我一直以为& q只是传递q的地址,它如何将q的值转换为int sqp(int * x)?
I'm still confused with using & to pass the value of pointer into a function. I always thought &q is just passing the address of q, how is it going to translate the value of q into int sqp(int * x)?
推荐答案
没有翻译".
您的功能
int sqp(int *x);
接受一个参数x
,其类型为int *
,即指向整数的指针".
takes one argument, x
, whose type is int *
, i.e. "pointer to integer".
因此,要调用它,您需要向其传递一个指向整数的指针,即整数的地址.
So, to call it you need to pass it a pointer to an integer, i.e. the address of an integer.
&
前缀运算符用于计算其参数的地址,因此您可以执行以下操作:
The &
prefix operator is used to compute the address of its argument, thus you can do:
int foo = 4711;
sqp(&foo);
使用变量foo
的地址调用sqp()
函数.
to call the sqp()
function with the address of the variable foo
.
这篇关于为什么要使用&用*传递函数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!