简体   繁体   English

价值传递中的困惑vrs参考/指针传递

[英]Confusion in pass by value vrs pass by reference/pointer

Following declaration 申报后

 void insert_LL(Node *head, int data);

when called, a copy of head pointer reaches the function, so if a node is added at the head, the value of the head is changed. 调用时,头指针的副本到达函数,因此,如果在头处添加节点,则头的值将更改。 But, as we have the local copy of head so the actual head pointer isn't changed. 但是,由于我们具有head的本地副本,因此实际的header指针不会更改。 So we declare it as follows 所以我们声明如下

 void insert_LL(Node **head, int data);

My question: 我的问题:

  1. Is the above explanation correct? 以上解释正确吗?

  2. If yes, so that means in C, it is always a pass by value (a copy of pointer is reached in the function) which is same as in Java. 如果是,则意味着在C语言中,它始终是值传递(与函数中的指针副本相同),与Java中相同。 Am I correct? 我对么?

  3. If yes, so how does pass by reference/pointer come into the picture? 如果是,那么如何通过引用/指针进入图像?

1- YES 1-是

2- YES 2-是

3- In C, Pass-by-reference is simulated by passing the address of a variable (a pointer) and dereferencing that address within the function to read or write the actual variable. This is referred as "C style pass-by-reference.". 3- In C, Pass-by-reference is simulated by passing the address of a variable (a pointer) and dereferencing that address within the function to read or write the actual variable. This is referred as "C style pass-by-reference.". In C, Pass-by-reference is simulated by passing the address of a variable (a pointer) and dereferencing that address within the function to read or write the actual variable. This is referred as "C style pass-by-reference.".

Pass by reference is not the term to be used in C. The address value of the pointer variable is passed. 按引用传递不是C中要使用的术语。指针变量的地址值被传递。 The compiler stores references to the variables with the names given to the memory locations where the variable is held. 编译器使用对变量的引用进行存储,并为变量保留的存储位置指定名称。

The major difference between a reference and a pointer is that "pointer" can take any address value unless made "const". 引用和指针之间的主要区别在于,“指针”可以采用任何地址值,除非将其设置为“ const”。 Whereas when you pass by reference, you can't. 而当您通过引用时,您就不能。

What are the differences between a pointer variable and a reference variable in C++? C ++中的指针变量和引用变量之间有什么区别?

This thread will do all the needful. 该线程将完成所有需要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM