简体   繁体   中英

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. 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. Am I correct?

  3. If yes, so how does pass by reference/pointer come into the picture?

1- YES

2- YES

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. 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". Whereas when you pass by reference, you can't.

What are the differences between a pointer variable and a reference variable in C++?

This thread will do all the needful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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