The declarations below set up two arrays and one pointer variable. Array declarations and definitions permit the storage of large quantities of data in computer memory.
char ch[20]; double b[5]; int *pi;The first statement gets the compiler to layout a region of memory big enough to hold 20
char
values stored in consecutive cells.
The symbol ch
is
a constant, it is the address of the first cell in the region. The second
declaration gets the compiler to layout a region of memory to hold 5
double precision floating point values stored in consecutive cells.
The symbol b
is a constant, the address of the first cell
in the region.
The third declaration sets up a variable, pi
, to store an
address of an
int
.
The reasoning behind this is that the third declaration
declares that the dereference of pi
, namely,
*piis an
int
. So if the dereference of pi
is an
int
, then pi is a place to store the address of an int. This is
THE WAY in C/C++ to declare that a variable has type
"address of an int
". Variable pi
is said to be a
pointer variable. Once an address is stored in pi
, we
say that it "points to" the storage location with that address.
IMPORTANT: Symbols
ch
and
b
do stand for addresses in computer memory. But the 20 places
starting at
ch
and the 5 places starting at
b
do NOT contain meaningful data; They have not been initialized.
Furthermore variable
pi
does not contain a meaningful address because it has not been initialized.
IMPORTANT: Variable pi
contains garbage
before an address value has been put in it. DO NOT USE *pi
until pi
has been assigned a value!
IMPORTANT:
Once variable pi
contains a
meaningful address, the expressions pi
and
*pi
represent TWO DIFFERENT places in memory.
Variable pi
is a storage location we use to store an address
(of an int). Expression *pi
is a storage location we will
use to store an integer value.
Now consider the following definitions:
char ch[20] = "BAC"; int ages[] = {14, 5, 9}; float wt[] = {100.1, 43.4, 77.2}; char *names[] = {"Albert", "Beth", "Carl"}; int *pi = ages;The first four definitions set up and intialize four arrays. The first array,
ch
is an array of
char
of size 20 whose first 4 positions are intialized to values
66, 65, 67, 0
when written as decimal values.
The second array defined is
ages
This is an array of
int
of size 3. The brackets ([]) following
ages
is what tells the compiler that
ages
is to be an array.
Since the programmer did not put a number between the brackets []
the compiler determines the size of the array by counting
the number of items (3) between the braces. It then stores 14, 5, and 9
in those 3 positions. These numbers are at indicies 0, 1, 2 respectively.
That is ages[0] refers to the 14 and so on.
The definitions of arrays
wt
and
names
work similarly, but let's talk a bit more about
names
.
What appears to the left of each array name is the type of its entries.
Array ages
is to store integer values (int
).
Array wt
is to store single precision floating point values
(float
).
Array
names
is to store values of type
char *What's this? When you dereference the value you get a
char
. So each entry in array
names
is to be the address of a char. The value names[0]
is "Albert"
.
And "Albert"
as explained in the last page is the address of
its first character. So it is the address of a char
! Similarly
for
names[1]
and
names[2]
.
DANGEROUS BEND. Convenient for programmers. Confusing for beginners!
We come to the definition of pi
:
int *pi = ages; //Comment: it looks like value ages is assigned to *piThis is short hand for:
int *pi; pi = ages;So the address symbolized by
ages
is being put into
pi
. It is NOT being put into *pi
!!
This is a special usage. It only works in definitions of pointer variables.
Once variable pi
has been defined, the statement:
*pi = expressionassigns the value of the expression to the storage location
*pi
. For example, given the definitions above, the first line
below stores 666 in array ages
in place of the 14.
The next statement increases
the address in variable pi
by 1 (int), so pi
now contains the address of the 5 in array ages
. Finally, the
last statement replaces the 5 with 999.
*pi = 666; pi += 1; *pi = 999;
Next: Function: fgets