DAY35
Wednesday, 01 August 2012
Today we might talk about pointers, addresses, arrays and strings.
-
The name of an array like "a" is a pointer to the first element of the array,
and we aren't allowed to change such a pointer.
-
The values in the array are retrieved by expressions like "a[3]".
-
If we have created an array "a", we can make a pointer "p", and
have it point to that array by a simple statement like "p=a".
The pointer "p" can be changed. We can have it point to the SECOND
entry of "a" by the statement "p=p+1", and we can reset it to point to
an array "b" by "p=b". (example: print_array.c).
-
Pointers can be used to avoid setting up temporary arrays. For instance,
we might want to select and print just one array. We could do this
by repeating the code for each possibility, or by copying the selected
array into a temporary array, but a good choice would be to simply
"point" to the array you want. (example: choose_array.c)
-
An iteration might involve many steps in which the next value of an array
is computed from the previous value. After each step, then, we would normally
have to copy the new information into the old array, so that the new array
is ready to store the next set of information. Instead, we could use
pointers that simply alternate between two arrays. (example: alternate.c)
-
We can create and destroy arrays dynamically using the malloc() and free()
functions. The malloc() function returns a pointer to a given amount of memory,
which can be indexed like an array, or accessed using the pointer.
(example: create_array.c)
-
The name of an string like "s" is a pointer to the first character
in the string. To print all the characters in a string, we can make
a pointer that starts at address "s", and then increments by 1 until
it points to the '\0' character. (example: print_string.c).
-
A pointer to a string can be used just like the name of the string. But we
can also make the pointer point to the second or third character.
The printf() command will print a string up to
the null character; this means that if we modify the pointer to point to the
second character in a string, the printf() command will print out the string
except for the first character. (example: print_string.c)
-
Text processing is easy with strings. As an example, we can create a
function that copies the information from one string into another.
We assume the strings are null-terminated. The function doesn't know
how long either string is, it just has pointers to their starts.
(example: copy_string.c)
Programs and functions we might talk about:
-
alternate.c solves a tiny version of the heat equation.
We have old data in one array and new data in another. One approach copies
the new data into the old array after each step. The other uses pointers so
that no data moves.
-
choose_array.c prints the array
with largest sum. We look at three ways of doing this, one of which
involves pointers.
-
copy_string.c shows how pointers can be
used to copy a string.
-
create_array.c uses the malloc() and free()
commands to create an array of any size, and then to free up the memory
when the array is no longer needed.
-
print_array.c shows how a pointer can be
used to point to an array. If we don't want to point to the first entry
of the array, we can increment the pointer. If we want to change an
element of the array, we can use the pointer to do so.
-
print_string.c shows how a pointer can be
used to point to a string.
Last revised on 01 August 2012.