This class was on Pointers and Functions.
We analyzed the problem posed at the end of Class 14 - how to write a modular, structured program to reverse a string in place. There were three modules in the program: calculate string length, reverse the string in palce, and print the new string. The last is easy so we'll concentrate on the first two. We discussed the technique of writing code in small modules and testing each module with it's own test program. See last year's Problem4 for another example of how this is done.
This program was developed, written, and tested in class.
This program was developed, written, and tested
in class. The two algorithms - length and reverse - were combiined into
the final program both as separate functions and as separate structures
within main()
. The results were demonstrated
in class.
These programs show some of the features of string initialization and
show that the two string declarations are not exactly equivalent. The str1[]
declaration produces an address, str1
, which can be used to
access the elements of the array. But, str1
cannot be used
as an lvalue (put on the left side of the assgnment operator =
). because the contents of the string constant it points to cannot be altered.
The *str2
declaration produces a pointer - a real place in
memory that holds an address, not just the address itself. Thus str2 can
be used as an lvalue. The top program and a slight varation shown
in class demonstrate that str2
is an lvalue but str1
is not. The question of whether an array of ints
(or floats
or doubles
) can be declared and initialized using the *b
declaration was left as an open question. The code is attached
below if you want to try for yourself.
Pointers as Function Parameters
We discussed what actually happens when a pointer is passed as a function
parameter. This function, which converts between temperatures in C and F,
was demonstrated in class wth a more elaborate
main()
.
Programs These programs were shown in class.
![]() WPI Home Page |
[cs1005] [Classes] |