REFERENCES in C++
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about why a function parameter changes when passed by reference, as a short visual explanation of the "&" syntax.
Recommended by 1 people across 1 different months, 2019 to 2021.
What it is
A 10-minute beginner tutorial from The Cherno's C++ series explaining what references are, how they differ from pointers, and how they let functions modify variables passed in. It is presented by The Cherno with live coding examples.
What people said
Recommends Narice · 2019-01 · Stack Overflow · on “What happens to a parameter if it is passed twice? Once by value and once by reference? Will it be modified or not?”
This question is related to the behavior of functions parameters and reference/pointers. When you pass a value to a function, it copies the value you give to a local variable inside the function. in your case, when you pass 's', it copies it to 'a' like if you would do it in your main: int a = s (this is what is going on) The same applies to the reference. when you pass 's' to the third parameter, it does this: int &rez = s When you pass it by reference, it is sort of taking the address of the variable instead of the value itself and copying this address to a local variable that is already dereferenced. (pointer stuff) So the value is changed. See the video related to pointers and then refer
Cites Aritro Shome · 2021-03 · Stack Overflow · on “Why does the post increment operator not increment the actual value of stored at an integer pointer?”
I have the following code : #include<iostream> #define LOG(x) std::cout << x << std::endl; void Increment(int* a) { *a++; } int main() { int a = 8; int& ref = a; ref = 2; Increment(&a); LOG(a); } On running the executable, however, the output is 2 and not 3. I understand that *(pointer) gives the value stored at the pointer and (operand)++ increments the value of the operand and stores it in the memory address of the operand itself. So, what is the exact cause of this unexpected result ? COMPILER USED : g++ (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 10.2.0 Copyright (C) 2020 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO wa
Cites Marcus Cazzola · 2021-12 · Stack Overflow · on “Why is C++ function's inputs become outputs in the blueprint?”
The reason why the input is becoming the output is because you are using the “&” sign. The “&” means that the variable you are passing in, have the same memory address, as the the one you are calling the function with. Here is a video about references: c++ references video [this video]