Content
Binary and hex Address-of operator (&) Dereference operator Pointers and References <- Go BackUniversity of Michigan at Ann Arbor
Last Edit Date: 01/09/2023
Disclaimer and Term of Use:
We do not guarantee the accuracy and completeness of the summary content. Some of the course material may not be included, and some of the content in the summary may not be correct. You should use this file properly and legally. We are not responsible for any results from using this file
This personal note is adapted from Professor Amir Kamil, Andrew DeOrio, James Juett, Sofia Saleem, and Saquib Razak. Please contact us to delete this file if you think your rights have been violated.
This work is licensed under a Creative Commons Attribution 4.0 International License.
The one data format computers really care about
Strings of bits
Like decimal numbers but, with only 1's and 0's
Note: Adding zeroes to the left does not change the value. For example: 001492 = 1492 and 001011 = 1011.
Convert binary to decimal
Let's convert binary 10110101 to decimal.
$$10110101 = 1 \times 2^0 + 0 \times 2^1 + 1 \times 2^2 + 0 \times 2^3 + 1 \times 2^4 + 1 \times 2^5 + 0 \times 2^6 + 1 \times 2^7$$$$~~~~~~~~~~~=1 \times 1 + 0 \times 2 + 1 \times 4 + 0 \times 8 + 1 \times 16 + 1 \times 32 + 0 \times 64 + 1 \times 128$$$$=1+0+4+0+16+32+0+128~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~$$$$=181~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~$$Hard to read long strings of nothing but 1’s and 0’s
Break it up into groups of 4 bits called nibbles
Take each 4-bit group as a value from 0 to 15 (Values 10 to 15 written as A to F)
Often notated with 0x
in computers
Convert binary to hex
Let's convert binary 10110101 to hex.
$$10110101 = 1011~0101 = \text{B5}$$Convert hex to decimal
$$\text{B5} = 5 \times 16^0 + 11 \times 16^1$$$$= 5 \times 1 + 11 \times 16$$$$= 5 + 176~~~~~~~~~~~~~$$$$= 181~~~~~~~~~~~~~~~~~~~~$$The address-of operator (&) let programers have the ability to query the address on an object once it has been created.
Take the following code as an example:
1 int x = 3; 2 int &r = x; 3 cout << x; // prints 3 4 cout << r; // prints 3 5 cout << &x; // prints 0x804240c0 6 cout << &r; // prints 0x804240c0
The process should look like the following pcture.
Don't confuse the address of operator with a reference type
int x;
x
is a variable whose type is int
int &r = x;
r
is a variable whose type is reference-to-int
int swap(int &a, int &b);
a
and b
are variables whose type is reference-to-int
Addresses can also be stored in a category of objects called pointers. A pointer variable can be declared by placing a *
symbol to the left of the variable name in its declaration.
Take the following code as an example.
1 int x = 7; 2 int *ptr = &x; 3 cout << *ptr; // prints 7 4 cout << ptr; // prints 0x804240c4
The process should look like the following pcture.
Don't confuse the dereference operator with a pointer type
int x = 7;
x
is a variable whose type is int
int *ptr = &x;
ptr
is a variable whose type is pointer-to-int
cout << *ptr;
The dereference operator "follows the pointer" to the object it points to
The x
and &
symbols mean different things when they are used as part of a type and when they are used in an expression;
When used in a type, *
means that the type is a pointer type, while &
means the tyoe is a reference type.
When used as a unary prefix operator in an expression, *
is used to deference a pointer, while &
ise used to obtain the address of an object.
When used as a binary infix operator, *
is multiplication, while &
is a bitwise and operation.
Example 1:
1 int x = 3; 2 int *ptr = &x; 3 int &ref = *ptr;
In the second line, *
is used as a type, so ptr
is a pointer to an int
. The initialization is an expression, so the &
obtains the address of the object corresponding to x
.
In the third line, &
is used as part of the type, so ref
is a reference variable.
Its initialization is an expression, so the *
dereferences ptr
to get the object associated with x
.
The result is that ref
is an alias for the same object as x
.
Example 2:
1 void swap_pointed(int *x, int *y) { 2 int tmp = *x; 3 *x = *y; 4 *y = tmp; 5 } 6 7 int main() { 8 int a = 3; 9 int b = 5; 10 swap_pointed(&a, &b); 11 cout << a << endl; // prints 5 12 cout << b << endl; // prints 3 13 }
Try it out:
Click here to open in new windowThe whole process of the code above looks like the following diagram:
References can also be used to indirectly refer to other objects. However, a pointer actually holds the address of another object, while a reference just acts as another name for an existing object.
A reference must be initialized to refer to another object, and the association between the reference and its object cannot be broken.
On the other hand, a pointer need not be initialized to point to an object, and the address value it holds can be later changed to be the address of a different object.
A pointer can be null or have an undefined value, so it must be used carefully to avoid undefined behavior.
Note that both pointers and references allow objects to be used across scopes.