[英]What is wrong in passing pointer variable as an function argument instead of address of variable in C?
I know that if I pass &a as an argument in fun ,I will get result as *p =20 after fun is executed .Now see in the following code p is pointer variable passed as an argument to function fun .我知道,如果我通过与一个在有趣的说法,我会得到的结果* P乐趣执行= 20之后。现在在下面的代码为P看到的是作为参数传递给函数fun通过指针变量。 But after compilation I get result as * p =10 instead of 20 .
但是编译后我得到的结果是 * p =10 而不是 20 。
What is wrong in passing pointer variable p instead of address of a ie &a to function fun ?将指针变量p而不是 a ie &a的地址传递给fun函数有什么问题?
#include <stdio.h>
int a = 10, b = 20;
void fun(int *p)
{
p = &b;
}
int main()
{
int *p = &a;
fun(p);
//fun(&a);
printf("%d\n", *p);
return 0;
}
The function parameter p
函数参数
p
void fun(int *p)
{
p = &b;
}
is a local variable of the function.是函数的局部变量。 The function deals with a copy of the value of the pointer passed to the function
函数处理传递给函数的指针值的副本
fun(p);
So within the function it is its local variable that is changed.所以在函数内部,它的局部变量被改变了。
You may imagine the function and its call the following way (I will rename the function parameter to ptr
for clarity)您可以想象该函数及其调用方式如下(为清楚起见,我将函数参数重命名为
ptr
)
fun(p);
//...
void fun( /* int *ptr */ )
{
int *ptr = p;
ptr = &b;
}
As you can see the original pointer p
was not changed.如您所见,原始指针
p
没有改变。
If you want to change the pointer you need to pass it by reference.如果要更改指针,则需要通过引用传递它。 In this case the function will look like
在这种情况下,函数看起来像
void fun( int **p)
{
*p = &b;
}
and in main it is called like并且在主要它被称为
fun( &p );
In this case the pointer p
(and not a copy of its value) will be indeed changed in the function.在这种情况下,指针
p
(而不是其值的副本)确实会在函数中发生变化。
That is the general rule is if you want to change an object in a function you nned to pass it to the function by reference.这是一般规则,如果您想更改函数中的对象,则需要通过引用将其传递给函数。 In C passing by reference means passing an object indirectly through a pointer to it.
在 C 中,通过引用传递意味着通过指向对象的指针间接传递对象。 In this case dereferencing the pointer you will get a direct access to the original object.
在这种情况下,取消引用指针,您将直接访问原始对象。
The pointer identifiers p
in main and fun are not the same, ie do not refer to the same object. main 和 fun 中的指针标识符
p
不一样,即不指向同一个对象。 If in fun() you assign to p, you effectively do nothing .如果在 fun() 中分配给 p,则实际上什么都不做。 The value of the local parameter p is simply lost upon
return
.局部参数 p 的值在
return
丢失。
The function argument p
in fun
is a different object in memory from the variable p
in main
, and updating one does not affect the other. fun
的函数参数p
是内存中与main
的变量p
不同的对象,更新一个不会影响另一个。
It appears to work if you pass &a
because you initialize main:p
to be &a
before calling fun
.如果您通过
&a
似乎可以工作,因为您在调用fun
之前将main:p
初始化为&a
。 main:p
is not being updated at all. main:p
根本没有更新。
To do what you want, you have to pass a pointer to p
:要执行您想要的操作,您必须将指针传递给
p
:
void fun( int **p )
{
*p = &b;
}
int main( void )
{
int *p = &a;
fun( &p );
printf( "%d\n", *p );
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.