Reference a Pointer to Point to a Reference
Tags: codenewbie, cpp, c, beginners
Published: 2019-11-04 16:38:15

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](https://en.wikipedia.org/wiki/Low-level_programming_language) language with my caliber of mostly using [high level](https://en.wikipedia.org/wiki/High-level_programming_language) 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:

```cpp
int num = 3 //instantiate int variable

cout << &num;
//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](https://www.programiz.com/cpp-programming/pointers) 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."

[Pointers Source](http://www.cplusplus.com/doc/tutorial/pointers/)

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:

```cpp
int *num;//Valid Pointer
int* num2;//Valid Pointer

```
And to use the pointer with the reference.

```cpp
  int num = 3; //create variable

  int *ptr = &num; //create pointer and assign num variable's reference

  cout << endl << "Pointer: " << *ptr;  //Output: 3
  cout << endl << "Reference: " << &num;  //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:

```cpp
  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 = &num; //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](https://www.programiz.com/cpp-programming/pointers-arrays)
[Pointers with Functions](https://www.programiz.com/cpp-programming/pointers-function)
[Another Pointers with Functions](https://www.tutorialspoint.com/cplusplus/cpp_passing_pointers_to_functions.htm)
[Passing Reference to a Pointer](https://www.geeksforgeeks.org/passing-reference-to-a-pointer-in-c/)