On our machine
storm.cis.fordham.edu,
a memory address
(and a “pointer” variable that holds a memory address)
occupies 64 bits = 8 bytes.
That means it could take up to 64 ÷ 4 = 16 hexadecimal digits
to write the
address.
address.C,
address.txt
&
is a
unary operator
(i.e., an operator with only one operand)
like the negative sign
-i.
size_t
for a variable holding the number of elements in an array,
or the number of bytes in a block of memory.
pointerint.C,
pointerint.txt
int
into a “pointer to an int”.
pointerdouble.C,
pointerdouble.txt
double
into a “pointer to a double”.
Dereferencing
a pointer means getting the value of the variable
that the pointer points to.
A “stand-alone”
variable is one that does not belong to an array or a structure.
*
is a unary operator (i.e., an operator with only one operand)
that dereferences the pointer, i.e.,
gets the value to which the pointer points.
dereferenceint.C
dereferenceint.txt
dereferencedouble.C,
dereferencedouble.txt.
double
are output.
#include <iomanip>
and use the i/o manipulator
setprecision.
int a[] {0, 10, 20, 30, 40, 50, 60, 70, 80, 90};
const size_t n {size(a)}; //the number of elements in the array
a
in place of
&a[0].
a+1 instead of &a[1].
a+2 instead of &a[2].
a+i instead of &a[i].
a+n instead of &a[n].
neighborint.C
and
neighbordouble.C.
//Assume that the array starts at memory address 1000.
//The array contains 10 elements, each occupying 4 bytes on our machine.
//Therefore the array occupies addresses 1000 to 1039 inclusive.
int a[] {
0, //Occupies addresses 1000 to 1003
10, //Occupies addresses 1004 to 1007
20, //Occupies addresses 1008 to 1011
30, //Occupies addresses 1012 to 1015
40, //Occupies addresses 1016 to 1019
50, //Occupies addresses 1029 to 1023
60, //Occupies addresses 1024 to 1027
70, //Occupies addresses 1028 to 1031
80, //Occupies addresses 1032 to 1035
90 //Occupies addresses 1036 to 1039
};
Note that the byte at address 1040 is not part of the array.
It is just beyond the end of the array.
loop.C,
loop.txt
++
(or decrement
--)
will make the pointer point
to the next (or to the previous) element in the array.
On our machine, the increment adds 4 to to the pointer
because what the pointer point to (an
int)
occupies 4 bytes.
elementaddress.C,
elementaddress.txt
int (with
++p),
we are actually adding 4 to the value of the pointer.
int
occupies 4 bytes of memory.
rocket1.C:
overwrite four consecutive array elements.
Has to be seen to be believed.
rocket2.C:
The same program, but with an int
i
instead of a pointer p.
The statements
p[0] = '.'; //trailing puff of smoke
p[1] = '=';
p[2] = '=';
p[3] = '>'; //the nose cone
in
rocket1.C
are simpler than the statements
a[i + 0] = '.'; //trailing puff of smoke
a[i + 1] = '=';
a[i + 2] = '=';
a[i + 3] = '>'; //the nose cone
in
rocket2.C.
movingaverage.C,
movingaverage.txt.
compute the sum of five consecutive array elements.
p[-2] + p[-1] + p[0] + p[1] + p[2]
in
movingaverage.C
is simpler than the expression
a[i-2] + a[i-1] + a[i] + a[i+1] + a[i+2]
bubble1.C,
bubble1.txt.
Uses subscripts
i,
j.
bubble2.C,
bubble2.txt.
Uses pointers
p,
q.
if (q[0] > q[1]) {
in
bubble2.C
is simpler than the statement
if (a[j] > a[j + 1]) {
in
bubble1.C.
struct.C,
struct.txt.
*
operator to
dereference a “pointer to a structure”.
.
operator get the value of each field of the structure.
struct.C,
struct.txt
pass.C,
pass.txt.
readwriteref,
is bad!)
f
receives a copy of the variable
i,
and can change the value of the copy.
cop.)
i
up in
main.
readonlyref
is just another name for the variable t.
t
is created.
const,
the function
f
cannot use
readonlyref
to change the value of
t.
j,
and can use this address (dereferenced with a
*)
to install a new value into
j.
f(i, s, t, &j);)
makes it obvious that the function can change the value of
j.
array.C,
array.txt.
passstruct.C,
passstruct.txt.
month is
before we could tell the computer what
f is.
You can insert the keyword const
into a declaration
in two places:
int i {10};
int j {20};
int *p {&i}; //p points to i.
The following diagram shows two regions of memory, the pointer on the right and the pointed-to variable on the left.
const.C.
Not allowed to change the value of a
const
variable.
It always holds the same value.
c++
compiler will say
const.C:18:11: error: increment of read-only variable ājā
constptr.C.
Not allowed to change the value of a
const
pointer.
It always points to the same place.
readonly.C.
A read-ony pointer is a needle that can’t scratch the record.
both.C.
A pointer that is const in both ways.