No. Consider the following character array:
char data[4] = {‘A’, ‘B’, ‘C’, ‘D’};
In this case we have an array containing four characters. It does not contain a string. There is no end-ofstring
character.
How can the compiler tell the difference between a character array and a string? It can’t. If we tried to
treat data as a string, the compiler would let us. We would, however, get strange results.
Question on Slide 9 of the slides
The variable width is undefined because it is in the middle of a comment. Check out the comment end
on the previous line.
Question on Slide 18 of the slides
The question is, why does array[2,4]print out a funny value? C++ uses the syntax array[2][4]
Page 15
to select a single element of the array.
When C++ sees the expression 2,4 it uses the comma operator (see Chapter 28, C++’s Dustier
Corners) to evaluate the expression. The result is 4. So array[2,4] is the same as array[4]. This,
unfortunately, is a pointer and C++ prints it as a pointer, which results in the strange printout.
Question on Slide 32 of the slides
Answer “c.” I’m a great believer in practical programming and follow the adage. “If you don’t write
stupid code, you don’t have to answer stupid questions.”
The “real” answer to this question, according to the ANSI draft standard, is that there are three flavors of
characters, char, unsigned char, and signed char, and compilers should generate a warning if you try to
mix flavors.
Many real compilers don’t follow the standard. Some generate signed characters, some unsigned, and
the Borland compiler has a switch that allows you to specify signed or unsigned as the default.
Side Effects
If possible, include a story from your programming experience titled “How I found a nasty bug caused
by a side effect and why I want to strangle the programmer who put it in.”
Review Questions
1. What is the difference between an array and a simple variable?
A simple variable holds only one value. Arrays hold many values.
2. What is the number of the last element of the following array?
int test[5];
“ 4” (The elements go from 0 to 4).
3. What's the header file that's used to bring in the data definitions for C++ strings?
#include <string>
4. How do you concatenate two C++ style string?
Use the “+” operator.
Page 16
5. What is the difference between a C style string and an array of characters?
Arrays of characters are fixed length. C++ makes no restrictions on what can be placed in any
Practical C++ Programming by manish baranwal
Start from the beginning
