Pointers and References (Part 2: When and How to use them)
Note: This post assumes you know the basics of programming, programming language C/C++ and read Part 1: Declaring and assigning value. This blog is for those who want to learn how variables are stored, how pointers and references work, when and why to use them. We use a function in our code to make it modular and intuitive. To understand how parameters in function are stored in memory, let's start with how a function call works through the following example. void print(int a) { cout << a; } int main() { int a = 10; print(a); return 0; } In memory, there is a call stack to keep track of active/running function calls in a program. When we start our program, stack frame for main is loaded in the call stack. When execution reaches to function call to 'print' function. Then stack frame for 'print' function is loaded in to call stack as well. The interesting thing happening here is, 'print' function has it's own v...