It's All About Pointers and References 1
It's All About Pointers and References 1
1
It’s All about Pointers and References 1
“We had to address information technology in the ways we had not before
and give the agents the tools that they need to do their job more efficiently
and more expeditiously.”
– Robert Mueller.
Ever wondered if you can do more than just implement things straight
forward? Ever wanted to dive into more technical side of C++? Now that we
have completed the fundamentals of C++ and set its difference from the
original C language, we will explore the low-level details of C++. In this
module, we will tackle the more technical workings that made C and C++
more popular compared to the rest of the programming languages in their
time. In addition, these concepts are what set C and C++ apart in technical
perspective of implementation.
So, what are we waiting for? Let us continue our exploration of the world of
Computer Programming.
Course Module
Recall
Since the start of the course, we have explored the C++ programming
language in its beauty as a high-level programming language. We have
altogether explored the conditionals, looping, arrays, functions and the
Object-Oriented Programming schemas of C++.
Low-level programming languages also lets the developer explore the world
of embedded systems, giving them access to even the smallest component in
the environment. Developers are given direct access to the memory of the
computer, particularly the random-access memory. They can also manipulate
the values stored in other memory sectors. There exist the least limitations
passed to low-level programming languages.
Course Module
Mid-level programming languages showcase user-friendly implementations
and structures. Majority of the features available to low-level programming
languages are hidden in the background and are usually taken for granted.
Some of the implementations are hidden from the user. In addition, some of
the tasks are designed by the programming language to be abstract.
Now that we have established the fact that C++ can be considered a low-level
programming language, we need to dwell on one of the infamous low-level
capability of C++ - the pointers and references. These features of C++,
coupled with dynamic memory allocations, form the fundamental of C++
being a low-level programming language.
Pointers
Pointers are special variables in C++ that "points" to a sector in the memory.
These variables are used as companions of the other variables. They extend
the coverage of the variables to such a point that the original variables are
being accessed from different sections of the code without being recreated
and reassigned.
Computer Programming 1
5
It’s All about Pointers and References 1
In this point, the concept behind pointers might not be too clear. For
example, we set a value to an int myID variable. If we create a pointer int
*ptrID with the address of int myID, any changes we do to the int myID
variable also happens to the int *ptrID pointer. And any changes we do to
the int *ptrID pointer also happens to the int myID variable. This is in
contrast with the usual workings of variables.
For example, if we assign to another variable int yourID the value of int
myID, then when changes occur to one of them, that said change is only done
to that specific variable. For instance, we assigned the constant 2 to int myID
variable, then the value assigned to the int yourID variable will not change.
And the same thing happens vice-versa. The change is isolated.
Pointers create somewhat like a mirror image of the variable they are
pointed to. Changes are shared between the two. It is also possible to create
multiple pointers pointing to the same variable.
As seen in Figure 1, we have declared a variable int myId and a pointer int
*ptrId. The pointer is then assigned the value of the address of the variable.
More on this later. We then displayed the values of the int myId variable and
the int *ptrId pointer. If we run this source code, the value of int myId
variable is 19 while the value of the int *ptrId pointer can be any address
Course Module
value like 0x071Ww1. To retrieve the value of the variable our int *ptrId
pointer is pointed to, we append an asterisk (*) to the pointer. In that line, we
will have the value of 19.
Figure 2. (2013). C++ Programming Language Pointers, References and Dynamic Memory Allocation
[Online Lecture]. Retrieved from
https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp4_PointerReference.html.
A pointer is then assigned with the address of the variable which is done by
appending an ampersand (&), the address of operator, before the variable
name. The address of the variable is in the format 0xSSSSSS where an S is
alphanumeric. As seen in Figure 2, remember though that the address of the
variable and pointer differs. The value stored in the variable is the actual
value whereas the value stored in the pointer is the address of the variable it
is pointing to.
References
We have used the address of (&) operator to retrieve the address of the
variable. In addition to that, we can also use the address of (&) operator like
pointers. That is a reference. A reference is a special variable which, like
pointers, stores the address of a variable. The main difference is that the
address stays permanent, whereas pointers can change the variable where
they point to.
Like a pointer, changes done to the original variable where a reference was
pointed to will also affect the output of the reference. This is also vice-versa.
Changes done to the value of the reference will change the value of the
variable it points to. Since this is the case, when we assign another variable to
the reference, what we are doing there is copying the value of the variable
and storing it to the reference and to the original variable the reference is
pointing to.
Computer Programming 1
7
It’s All about Pointers and References 1
Later, in the code, we changed the value of the reference int &refId
followed by the changing of the value of int yourId variable. If we display
the values now, we will get 856091, 756231 and 856091 respectively. Notice
how the variable int myId and the reference int &refId changed their
values however was unaffected by the change of the value of variable int
yourId.
Course Module
Glossary
High-level Programming Languages: A set of programming languages that
exhibits the strongest abstraction and farthest memory and operating system
control.
Low-level Programming Languages: A set of programming languages that
exhibit the weakest abstraction and closest memory and operating system
control.
Mid-level Programming Languages: A set of programming languages that
exhibit strong abstraction and some memory and operating system control.
Pointers: Special types of variables that store the address in the memory
rather than the actual value or constant wherein the address can change
depending on the assignment of another variable.
Address Of [Operator]: A unary operator (&) which retrieves the address of
a variable.
Address (Variable): The logical address of a variable in the memory.
References: Special types of variables that store the address in the memory
rather than the actual value or constant wherein the address is permanent
and unchanging.