【问题标题】:pointer argument receiving address in c++?c++中的指针参数接收地址?
【发布时间】:2014-03-06 07:26:46
【问题描述】:
int y=5;
int *yPtr = nullptr;
yPtr = &y;

我知道指针存储的是 y 的地址。并调用 *yPtr 取消引用 y。

如果我调用了 void 函数:

int main()
{
    int number = 5;
    function( &number );
}

void function( int *nPtr)
{
    *nPtr = *nPtr * *nPtr;
}

如果函数将指针作为参数,那么函数调用如何使用地址? 我知道 nPtr 存储地址,但为什么不能将其定义为。

void functions (int &ref)
{
    ref = ref * ref;
}

我的主要问题是:为什么接收地址参数的函数需要指针参数来接收地址?

【问题讨论】:

  • 我想这就是标准的编写方式。尽管引用主要用作指针,但您不会使用地址初始化它们,而是使用变量的值初始化它们。编译器可能在这两种情况下都会做同样的事情。
  • 所以你问为什么你需要显式传递一个地址,而不是编译器隐含地为你做这件事,就像它在传递引用的情况下所做的那样?
  • 我想知道为什么函数传递一个地址参数但有一个指针参数。
  • 因为指针持有地址...回想一下,指针的行为与任何其他普通类型完全一样:当我们复制指针时,指针的值也会被复制。复制后,两个指针是不同的。
  • 我想我知道我为什么感到困惑了。因为我认为该函数正在分配 *nPtr = &number,而不是像顶部的声明那样分配 nPtr = &number。哪个是对的?

标签: c++ pointers address-operator


【解决方案1】:

通过使用传递引用参数,您强制您的函数不是复制参数本身的值,而是使用您提供的实际变量。因此,要获得更清晰的视图,请参见下文:

void function( int number )
{
  cout << number;
}

function( myInt ); // function will copy myInt, into its local variables stack

但是,通过使用传递引用的方法,像这样:

void function ( int & number )
{
  cout << number
}

function( myInt ); // function will not copy myInt into its local variables stack, instead, it will use the already existent myInt variable.

编译器如何使用传递指针和传递引用参数没有区别。相反,您的函数调用将如下所示:

void function_p( int *number )
{
  cout << *number;
}

void function_r( int & number )
{
  cout << number;
}

// and the calls

function_p( &myInt ); // it is required to use address-of operator here
function_r( myInt ); // the effect will be the same, but with less effort in writing that address-of operator

在 C++11 中,程序员开始使用 pass-by-reference 方法,一般来说,通常是因为它更容易编写“模板”。


为了完成您的问题的答案,*&amp; 运算符仅引用参数的类型,以便它们创建复合类型。复合类型是根据另一种类型定义的类型。 C++ 有几种复合类型,其中两种是引用和指针。

你可以理解,它们只影响变量的类型(在我们的例子中,参数),通过以适当的方式编写它们:

int* p1; // we will read this: pointer p1 points to an int
int* p2 = &var1; // we read this: pointer p2 points to int variable var1

int var1 = 12;
int& ref1 = var1; // and we read this: ref1 is a reference to var1

您通常可以认为引用代表同一内存块的不同引用。

【讨论】:

  • 所以 * 和 & 运算符只指变量的类型而不是参数的实际值?出于可视化原因,我可以将其视为: void function_p( (int *)number ) 和 void function_r( (int &) number )
  • 完全正确。 *&amp; 表示创建复合类型的方法。比如int是普通类型,int*是复合类型
  • 有道理!我很困惑,因为我认为 function_p 正在分配 * number = &myInt.现在我明白 int * 只是类型和那个数字 =&myInt.
【解决方案2】:

你的意思是

void functions (int &ref)
{
    ref = ref * ref;
}

这就是你使用 refs 的方式,避免所有 '*' 的指针语法

【讨论】:

    【解决方案3】:

    这是 C++ 的另一个古怪之处。通过引用传递与传递指针不同。当你做类似的事情时

    void functions (int &ref)
    

    您可以将实际变量传递给functions(而不是指向它们的指针)

    int a = 12;
    functions(a);
    

    在函数内部,您无需取消引用。请注意,您传递的是 by 引用,而不是传递 a 引用。

    当你传递一个引用或指向某个东西的指针时,你会使用星号

    void function( int *nPtr)
    

    因此您必须使用 * 取消引用

    还请注意,您可以通过将&amp; 放在变量(或常量)前面获得对变量(或常量)的引用,但您声明通过将引用或指针* 在它前面。同时,您还可以通过在指针前面放置 * 来取消引用指针。

    【讨论】:

    • 对不起,错了,你的编译器应该告诉你的。 functions 可以更改 ref,显然你不能更改 12。出于同样的原因,传递引用不传递实际的整数值,它传递对整数对象的引用。至于第二部分:引用和指针是两个不同的东西,你把它们弄糊涂了
    • 你说得对,我发帖的时候没想到
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 2021-09-06
    • 2021-12-24
    • 2020-07-06
    • 1970-01-01
    相关资源
    最近更新 更多