I have been intimidated by C++ for such a long time because of how long this language has been around and how it is a standard for most developers coming from a computer science background (I don't come from a traditional computer science background for context). I was not sure if I would have been able to tackle a low level language with my caliber of mostly using high level languages. Although, there is debate that C++ is sometimes being considered a high-level language, but it would then be kept as mid-level, as it could have traits of both low and high level languages. However, I knew I would want to try C++ at some point because many music & audio tools are built using this language. But that's besides the point. Point.
So, Pointers and Reference! Something I always heard about whenever I find topics about C++. Let's start with references.
Reference
What is a reference? Reference is the memory address of a variable. This uses the & operator with the variable name. Let's show how it looks:
int num = 3 //instantiate int variable
cout << #
//Output: 0x7ffcc1b8a35c
Why doesn't it return the integer 3? And what is that long bunch of characters? This is the location of memory address that is being stored into the computer data. Every variable instantiated gets stored into all these different memory addresses and we can 'reference' them using this reference operator.
The long address output, Programiz has a good explanation on that:
"The 0x in the beginning represents the address in hexadecimal form.
Notice that first address differs from second by 4-bytes and second address differs from third by 4-bytes.
This is because the size of integer (variable of type int) is 4 bytes in 64-bit system. "
Since working with Javascript & Ruby on Rails especially, I'd like to think of this analogy as every variable you are creating also auto-generates an ID to that. Then you can access those 'ids' like &variable_name. So how do we access these? We point to those references using Pointers.
Pointer
What is a pointer? According to the official Cplusplus website:
"...variables have been explained as locations in the computer's memory which can be accessed by their identifier (their name). This way, the program does not need to care about the physical address of the data in memory; it simply uses the identifier whenever it needs to refer to the variable."
So we can say that a pointer is what is used to access that reference.
The syntax for a pointer is adding an * next to the variable name or to the data type, like so:
int *num;//Valid Pointer
int* num2;//Valid Pointer
And to use the pointer with the reference.
int num = 3; //create variable
int *ptr = # //create pointer and assign num variable's reference
cout << endl << "Pointer: " << *ptr; //Output: 3
cout << endl << "Reference: " << # //Output: 0x7ffcc1b8a35c
You can see that when *ptr printed out 3, while &num printed the memory address of num. If we tried to access num as itself:
int *ptr = num;//ERROR: cannot initialize a variable of
//type 'int *' with an lvalue of type 'int'
//or if we tried to access the memory address without using a pointer:
int ptr = # //ERROR: cannot initialize a variable of
//type 'int' with an rvalue of type 'int *'
cout << *ptr;
It's invalid because you are trying to access one another without using the other syntax. I would say that the pointer and reference go hand-in-hand together for access, but there could possibly be exceptions to this.
I just wanted to a basic introduction with pointers and reference, but I can POINT to some good REFERENCES to learn more about this topic, since you can work with Pointers with structs, with arrays, with functions, passing pointers, passing references, etc.
Pointers with Arrays
Pointers with Functions
Another Pointers with Functions
Passing Reference to a Pointer
Top comments (0)