CS 225 Notes 3
Introduction
We will start looking in detail at a C++ program by looking at a specific
example
program. This program includes several ways to manipulate strings.
6 const int STRLEN=
50;
Constants in C++ are indicated like ordinary variables, with the keyword
const in front. This means that STRLEN can be used anywhere an integer
can be used but it can't be changed. So,
cout << STRLEN;
is legal. But
STRLEN=7;
is not.
These are better than using a #define like
#define STRLEN 50;
because the const has atype, so the compiler can tell if you are using
it where you shouldn't.
8 int str_length(char * str);
This is a function declaration. It will make more sense after we talk about
functions. Here it just tells the compiler that there is a function with
this name and arguments somewhere. This allows the compiler to process
uses of this function before it sees the code.
19
copy = new char[STRLEN];
Earlier in the program we defined copy as a character pointer. Here
we are creating a new chunk of memory and pointing copy at it. The new
keyword tells the computer we are going to need some memory. After
the keyword, we tell it what kind of thing we want memory for. In this
case, we want an array of characters. The value in the brackets tells new
how many elements to put in the array. So, after this line, copy points
to an array that can hold STRLEN characters. This means a string of length
STRLEN-1, since we need room for the NUL character at the end.
20
dest=copy; src=instr;
This just makes copies of the pointers to the two arrays.
We need copies since in the next section, we will alter the pointers
we are using and if we changed the original pointers, we would no longer
be able to find the beginning of the arrays.
21
while(*dest++ = *src++);
A lot is happening here. We will look at this almost character
by character.
*src
This is a pointer dereference. In the line above, this is
replaced by whatever the pointer src is pointing at. Initially, this is
the first character in the instr array.
*src++
The ++ part is called the autoincrement operator. It adds
one to the variable it is attached to. So it adds one to src. But, it does
this after src is used. So, first the pointer is rereferenced, the value
is gotten from the array, and then the value of str is changed. Adding
one to an array pointer means that it now points to the next element in
the array.
*dest++
This is similar. It first does the dereference, then increments
the pointer.
*dest++ = *src++
Now we put it all together. This assignment (=) is done in
the following order. This assumes that both src and dest are pointing at
the first elemenst of the arrays they point at.
The value src points to is fetched from the array (*src)
The value of src is changed (*src++)
The pointer dest is use dto find the element ot points
at (*dest)
The value is copied from src to dest (=)
The value of dest is changed (*dest++)
(*dest++ = *src++)
This the expression part of the while statement. If it is
true, the body of the while loop is executed. If it is false, the loop
exits and we start running at the next line. In C++, assignment has a value.
It is the value of the right hand side of the equal sign. In this case,
it is the value of *src. This is
before the increment. So we could do
char myflag= (*dest++ = *src++)
This would copy the value of *src into the variable myflag.
In C++, there are bool variables to hold true and false
values. But C had another way to tell true from false. If a number was
0, it was false, all other numbers where true. Since an assignment has
a value, we can check that value for true or false. If *dest++ = *src++
is non-zero, it is true and the body of the while loop is run. If it is
zero, the loop stops. So when would that expression be 0 or non-zero. *src
is a character. The numeric value of a character is its ASCII value. This
is non-zero for all characters, but one, The zero ASCII character is the
NUL character. So, the only time this expression is 0 is when *src retreives
the NUL character at the end of the array.
while(*dest++ = *src++);
Now we have the whole thing. This loop runs for each character
in the source string, copying it to the destination string, until it gets
to the NUL character at the end of the source string. Each time it sees
a character, it runs the body of the loop. But notice the code between
the expression and the semicolon. Its empty. This is legal in C++ and it
means do nothing. So the copying happens as a side-effect of the loop.
Several parts of C++ are designed together to make this
work. False is denoted by 0, strings are ended by an ASCII 0 and assignment
has a value.