|
C++ Programming Tutorials
Basics of C++
Structure of a
program
Variables
Data types
Constants
Operators
Basic Input/output
Control Structures
Control Structures
Functions (I)
Functions (II)
Compound Data Types
Arrays
Character Sequences
Pointers
Dynamic Memory
Data Structures
Other Data Types
Object Oriented Programming
Classes [I]
Classes [II]
Friendship & Inheritance
Polymorphism
Advanced Concepts
Templates
Namespaces
Exceptions
Type Casting
Preprocessor Directives
C++ Standard Library
Input/output with Files
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
C++ Programming Tutorials
Pointers
We have already seen how variables are seen as memory cells that can be
accessed using their identifiers. This way we did not have to care about
the physical location of our data within memory, we simply used its
identifier whenever we wanted to refer to our variable.
The memory of your computer can be imagined as a succession of memory
cells, each one of the minimal size that computers manage (one byte).
These single-byte memory cells are numbered in a consecutive way, so as,
within any block of memory, every cell has the same number as the
previous one plus one.
This way, each cell can be easily located in the memory because it has a
unique address and all the memory cells follow a successive pattern. For
example, if we are looking for cell 1776 we know that it is going to be
right between cells 1775 and 1777, exactly one thousand cells after 776
and exactly one thousand cells before cell 2776.
Reference operator (&)
As soon as we declare a variable, the amount of memory needed is assigned for it
at a specific location in memory (its memory address). We generally do not
actively decide the exact location of the variable within the panel of cells
that we have imagined the memory to be - Fortunately, that is a task
automatically performed by the operating system during runtime. However, in some
cases we may be interested in knowing the address where our variable is being
stored during runtime in order to operate with relative positions to it.
The address that locates a variable within memory is what we call a reference to
that variable. This reference to a variable can be obtained by preceding the
identifier of a variable with an ampersand sign (&), known as reference
operator, and which can be literally translated as "address of". For example:
This would assign to ted the address of variable andy, since
when preceding the name of the variable andy with the reference operator (&) we
are no longer talking about the content of the variable itself, but about its
reference (i.e., its address in memory).
For now on we are going to assume that andy is placed during runtime in the
memory address 1776. This number (1776) is just and arbitrary assumption we are
inventing right now in order to help clarify some concepts in this tutorial, but
in reality, we cannot know before runtime the real value the address of a
variable will have in memory.
Consider the following code fragment:
andy = 25;
fred = andy;
ted = &andy; |
The values contained in each variable after the execution of
this, are shown in the following diagram:

First, we have assigned the value 25 to andy (a variable
whose address in memory we have assumed to be 1776).
The second statement copied to fred the content of variable andy (which is 25).
This is a standard assignment operation, as we have done so many times before.
Finally, the third statement copies to ted not the value contained in andy but a
reference to it (i.e., its address, which we have assumed to be 1776). The
reason is that in this third assignment operation we have preceded the
identifier andy with the reference operator (&), so we were no longer referring
to the value of andy but to its reference (its address in memory).
The variable that stores the reference to another variable (like ted in the
previous example) is what we call a pointer. Pointers are a very powerful
feature of the C++ language that has many uses in advanced programming. Farther
ahead, we will see how this type of variable is used and declared.
Dereference operator (*)
We have just seen that a variable which stores a reference to another variable
is called a pointer. Pointers are said to "point to" the variable whose
reference they store.
Using a pointer we can directly access the value stored in the variable which it
points to. To do this, we simply have to precede the pointer's identifier with
an asterisk (*), which acts as dereference operator and that can be literally
translated to "value pointed by".
Therefore, following with the values of the previous example, if we write:
(that we could read as: "beth equal to value pointed by ted")
beth would take the value 25, since ted is 1776, and the value pointed by 1776
is 25.

You must clearly differentiate that the expression ted refers
to the value 1776, while *ted (with an asterisk * preceding the identifier)
refers to the value stored at address 1776, which in this case is 25. Notice the
difference of including or not including the dereference operator (I have
included an explanatory commentary of how each of these two expressions could be
read):
beth = ted; // beth equal to ted ( 1776 )
beth = *ted; // beth equal to value pointed by ted ( 25 ) |
Notice the difference between the reference and dereference
operators:
Thus, they have complementary (or opposite) meanings. A
variable referenced with & can be dereferenced with *.
Earlier we performed the following two assignment operations:
Right after these two statements, all of the following
expressions would give true as result:
andy == 25
&andy == 1776
ted == 1776
*ted == 25 |
The first expression is quite clear considering that the
assignment operation performed on andy was andy=25. The second one uses the
reference operator (&), which returns the address of variable andy, which we
assumed it to have a value of 1776. The third one is somewhat obvious since the
second expression was true and the assignment operation performed on ted was
ted=&andy. The fourth expression uses the dereference operator (*) that, as we
have just seen, can be read as "value pointed by", and the value pointed by ted
is indeed 25.
So, after all that, you may also infer that for as long as the address pointed
by ted remains unchanged the following expression will also be true:
NEXT >>
Declaration variables of Pointer Types
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers.
|