Programming Problems Using Structure 281
const Person const * pv1 = * pp1 ;1
Table 4.1 summarizes the different ways of using *. In this statement, the first * means that
pv1 is a pointer. The second * means dereferencing pp1. Dereferencing a pointer means
going to the address stored in pp1, and retrieving the value stored at that address. Since
pp1 is the address of another pointer (pp1 is the address of an address), pp1’s value is also
an address; * pp1 is a pointer and this address is assigned to pv1. For pv1, the names
and ages are obtained by using pv1 -> name and pv1 -> age. Please read this program
carefully and fully understand the reasons and the purposes for each *.
17.2 Packing Decimal Digits
17.2.1 Number Systems
The minimum unit of information in a computer is called a bit. One bit can have two
possible values: 0 or 1. A sequence of bits can be used to represent a binary number, a
number in base 2. This is a binary system. We usually think of numbers in base ten, a
decimal system. In base ten we form numbers with ten different values: 0, 1, 2, 3, 4, 5, 6,
7, 8, 9. What does 2783 mean in the decimal system? Two thousands + seven hundreds +
eight tens + three:
2 × 10
3
+ 7 × 10
2
+ 8 × 10 + 3 × 10
0
(17.1)
How does this relate to the binary systems? The number binary 10110 means:
1 × 2
4
+ 1 × 2
2
+ 1 × 2
1
+ 0 × 2
0
(17.2)
Another commonly used number system is the hexadecimal system. Hexadecimal num-
bers are in base sixteen, and thus require sixteen symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B,
C, D, E, F. What does EA29 mean in the hexadecimal system? It means
E × 16
3
+ A × 16
2
+ 2 × 16
1
+ 9 × 16
0
(17.3)
In C programs, hexadecimal numbers start with 0x (or 0X), for example, 0xAC, 0x9B, and
0x15. Sixteen is the fourth power of two and one hexadecimal digit can express a binary
number that is four bits long. A decimal number requires four bits. Table 17.1 shows the
relationships between the three different number systems.
In general, if a number system is base n, n symbols are allowed: zero, one, two, ..., n −1.
A number a
m
a
m−1
a
m−2
...a
1
a
0
means
a
m
× n
m
+ a
m−1
× n
m−1
+ a
m−2
× n
m−2
+ ... + a
1
× n
1
+ a
0
× n
0
(17.4)
How to convert between number systems? If a decimal number is 273, what is the binary
representation? 273 = 256 + 16 + 1 = 2
8
+ 2
4
+ 1. Therefore, 273
d
= 100010001
b
. Here,
the subscripts
d
and
b
indicate the numbers are decimal and binary respectively.
In C programs, the minimum size of a variable is one byte (8 bits) and its type is
unsigned char. The valid decimal values for unsigned char are 0 through 255, inclusively.
It is not possible to have a one-bit variable in C programs. Also, C does not allow for the
expression of binary numbers. For example, 10100110
b
is expressed as 0xA6 and 11001011
b
is expressed as 0xCB. Binary numbers may be used with gcc by prefixing 0b. For example,