在 C++ 中传递指针参数是按值传递吗?

发布于 2024-10-07 07:14:52 字数 124 浏览 7 评论 0原文

C++ 中传递指针参数是按值传递吗?因为我看到对指针的任何更改都不会反映在方法之外。不过,我通过取消引用指针所做的更改已得到反映。

在这种情况下,使用指针到指针作为函数的参数来修改函数内的指针值是否可以接受/标准过程?

Is passing pointer argument, pass by value in C++? Since i see that any change to the pointer as such is not reflected outside the method. The changes i do by dereferencing the pointer is reflected though.

In that case, is it acceptable/standard procedure to use pointer to pointer as argument to a function to modify the pointer value as such within a function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

叹沉浮 2024-10-14 07:14:52

两者皆是。

指针和其他东西一样按值传递。这意味着指针变量的内容(指向的对象的地址)被复制。这意味着,如果更改函数体中指针的值,该更改将不会反映在仍指向旧对象的外部指针中。但您可以更改指向的对象的值。

如果要将指针所做的更改反映到外部指针(使其指向其他内容),则需要两级间接(指针到指针)。调用函数时,可以通过在指针名称前放置 & 来完成。这是标准的 C 做事方式。

使用 C++ 时,优先使用引用而不是指针(此后也优先使用指针到指针)。

对于为什么引用应该优先于指针,有几个原因:

  • 与函数体中的指针相比,引用引入的语法噪音
  • 更少 引用保留的信息比指针多,对编译器可能有用

引用的缺点主要是:

  • 它们打破了 C 的简单传值规则,是什么使得理解函数关于参数的行为(它们会被改变吗?)变得不那么明显。您还需要函数原型来确定。但这并不比使用 C 时所需的多个指针级别更糟糕。C
  • 不支持它们,当您编写应与 C 和 C++ 程序一起使用的代码时,这可能会成为问题(但这不是最常见的情况) 。

在指针到指针的特定情况下,差异主要是简单性,但使用引用也可以很容易地删除两层指针并仅传递一个引用而不是指针到指针。

Yes to both.

Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the object pointed to) is copied. That means that if you change the value of the pointer in the function body, that change will not be reflected in the external pointer that will still point to the old object. But you can change the value of the object pointed to.

If you want to reflect changes made to the pointer to the external pointer (make it point to something else), you need two levels of indirection (pointer to pointer). When calling functions it's done by putting a & before the name of the pointer. It is the standard C way of doing things.

When using C++, using references is preferred to pointer (henceforth also to pointer to pointer).

For the why references should be preferred to pointers, there is several reasons:

  • references introduce less syntaxic noise than pointers in function body
  • references keep more informations than pointers, than can be useful for compiler

Drawbacks of references are mostly:

  • they break the simple pass-by-value rule of C, what makes understanding the behavior of a function regarding of parameters (will they be changed ?) less obvious. You also need function prototype to be sure. But that is not really worse than the multiple pointer levels necessary when using C.
  • they are not supported by C, that can be a problem when you write code that should work with both C and C++ programs (but that's not the most usual case).

In the specific case of pointer to pointer, the difference is mostly simplicity, but using reference it may also be easy to remove both levels of pointers and pass only one reference instead of a pointer to pointer.

伴我老 2024-10-14 07:14:52

我理解这里的混乱。 “按值传递”和“按引用传递”的概念虽然看起来很清楚,但并不那么清楚。
请记住,计算机不知道这些概念,并且不会按照这些概念行事。
计算机不知道这些类型。因此它不区分指针和值。
让我尝试通过示例来解释:

void func1(int x) //copy some value to local variable x (of type int)
{
   x = 5; //modify local variable. lost after function call
}

void func2(int *x) //copy some value to local variable x (of type int*)
{
   int a;
   x = &a; //modify local variable. lost after function call.
}

void func3(int *x) //copy some value to local variable x(of type int*)
{
   *x = 10; //x is local but *x is not! change is saved after function call!
}

func1 和 func2 是相同的。两者都修改局部变量。函数从堆栈弹出后,修改就会丢失。
func3 能够更改另一个内存位置(不是函数本地的变量)。

基本上,每个函数调用都是“按值调用”。但对于指针类型,我们有一种方法可以更改内存中远程地址的内容。

I understand the confusion here. The concepts of "pass by value" and "pass by reference" are not so clear even if they seem to be so.
Bear in mind that the computer does not know these concepts and does not behave according to it.
The computer does not know about the types. Hence it does not make a distinction of pointers and values.
Let me try to explain by and example:

void func1(int x) //copy some value to local variable x (of type int)
{
   x = 5; //modify local variable. lost after function call
}

void func2(int *x) //copy some value to local variable x (of type int*)
{
   int a;
   x = &a; //modify local variable. lost after function call.
}

void func3(int *x) //copy some value to local variable x(of type int*)
{
   *x = 10; //x is local but *x is not! change is saved after function call!
}

func1 and func2 are identical. Both modify a local variable. Modification is lost after function is popped off the stack.
func3 has ability to change another memory location (a variable which is not local to the function).

basically, every function call is "call by value". But in the case of a pointer type, we have a way to change the content of a remote address in memory.

那一片橙海, 2024-10-14 07:14:52

使用指针传递值
我将通过示例进行解释:

void f(int *ptr)
{
   cout<<*ptr;
}


int main ()
{
   int a=10;
   int *aptr=&a;
   f(aptr);
   return 0;
} 

这里,main函数中a是一个整型变量,其内容为10,地址为00F8FB04(假设)。
aptr是整型指针,存放的是整型变量a的地址,所以aptr内容就是整型变量a的地址,即00F8FB04。当我们将 aptr 作为函数参数传递时,只有 aptr 的内容(即地址)会复制到函数参数。
因此,ptr将收到aptr内容的副本(即地址00F8FB04)

Pass by value using Pointers
I'll explain it by example:

void f(int *ptr)
{
   cout<<*ptr;
}


int main ()
{
   int a=10;
   int *aptr=&a;
   f(aptr);
   return 0;
} 

Here, in main function a is an integer variable whose content is 10 and address is 00F8FB04 (assume).
aptr is pointer to integer, that store the address of integer variable a, so aptr content is address of integer variable a that is 00F8FB04. When we pass aptr as the function argument only content of aptr (that is address) are copies to function parameter.
So, ptr will receive the copy of content of aptr (that is address 00F8FB04)

阳光下的泡沫是彩色的 2024-10-14 07:14:52

如果您想更改指针本身,则可以使用指向指针的指针或对指针的引用。对于您最初的问题,从技术上讲,是的,所有参数都是按值传递的。

Either a pointer to a pointer, or a reference to a pointer, is what you would use if you wanted to potentially change the pointer itself. To your original question, technically, yes, all parameters are passed by value.

烛影斜 2024-10-14 07:14:52

是的,确实如此,就像 C 中的情况一样。

在这种情况下,使用指针到指针作为函数的参数来修改函数内的指针值是否可以接受/标准过程?

在什么情况下?你想要什么?您可以通过 & 修饰符使用真实引用。

void func(type &ref);

Yes it is, as it is in C.

In that case, is it acceptable/standard procedure to use pointer to pointer as argument to a function to modify the pointer value as such within a function?

In which case? What do you want? You can use real references with the & modifier.

void func(type &ref);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文