简体   繁体   English

在 C 中将指针变量作为函数参数而不是变量地址传递有什么问题?

[英]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.

相关问题 将指针的地址而不是指针作为 function 参数传递 - Passing an address of a pointer instead of a pointer as a function argument 将变量的地址传递给C函数 - passing address of variable to a C function 使用C中的函数获取指针中变量的地址 - Getting address of a variable in pointer using function in C 将指针地址传递给全局变量 - Passing pointer address to global variable eeprom空间中的变量地址作为C中的函数参数 - Variable address in eeprom space as function argument in C c-将指针传递给具有void * variable参数的函数 - c - passing a pointer to a function with a parameter of void* variable 将变量传递给函数将更改C中的地址位置 - Passing Variable To Function Changes Address Location in C 传递声明为函数指针的变量与传递未声明为指针的变量的地址一样吗? - Is passing a variable declared to be a pointer to a function the same as passing the address of a variable not declared to be a pointer? 将变量传递给函数的地址 - passing address of variable to function 在 C 中取消引用和将变量的地址分配给指针变量有什么区别? - What is the difference between derefencing and assigning the address of a variable to pointer variable in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM