C++ Tutorial

Chapter 2

PROGRAMMING IN C++

SECTION 1: ARRAYS
An array in C++ is a type of variable that can contain more than one value. For instance, say you need to keep track of a list of 100 numbers. From what you know now, the only way to do this is creating 100 variables and assigning values for each one. This is actually what an array does for you--it creates those 100 variables. The syntax for the simplest kind of array looks like this:
type name [ size ]
Say you want to create a hundred integers, this is how you'd do it:
  int integerList [100];
To assign a value to one of those integers (called elements of the array), you can do it like this:
  integerList[0] = 10;
  integerList[1] = 34;
  integerList[2] = 99;
  integerList[3] = 77;
  // ...
  integerList[98] = 88;
  integerList[99] = 233;
Note carefully that in C++ arrays always start with the 0 subscript, which is the 1st element of the array. By declaring an array with a size of 100, we are actually providing room for 101 elements! We usually don't access this 101st element (integerList[100]) because as you're about to see, the compiler sometimes set this value for us.

We use arrays everywhere in C++ programming. One of the most common uses of an array is in a string. A string is a variable that contains text--in this case the variable will be an array of char types. There are a couple things to be aware of however when you're dealing with strings. First, you must initialize a string to contain as many elements as you anticipate to have characters in the string. It's usually a safe idea to declare a string (an array of characters, remember) to have more than enough characters for what you need. Second, once you create a string, you'll be tempted to assign a bunch of text to it directly. I'll show you why this normally doesn't work, and what you should do instead.

With arrays, you don't nessesarily have to know the size of the array if you are going to assign a value to it immediately. For example, if you know what numbers you are going to put into an array, you can simply do this:

  int numbers[] = { 430, 31, 42, 53, 64, 75, 86, 3422, 443, 2342, 433, 2 };
Doing this is the same as creating an array of 12 elements and assigning each value individually, but this way is more efficient. You can also do this for an array of characters (a string):
  char mytext[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };
However, this is a very cumbersome syntax for defining text. C++ provides an easier way to do this, which is simply
  char mytext[] = "Hello World";
An interesting thing happens when you use this syntax. The quotes actually tell the compiler that you are going to be using this array of characters as a string instead of just a list of characters, so the compiler puts what is called a null terminator in the last element of the array (the element that you don't use). This null terminator has the ASCII value 0, and tells the compiler that this is the end of the string. C++ functions that read and print strings need some way of knowing when they should stop reading from the string. For example, a function that determines how long a string is would count all the characters in the string until it reaches this null terminator. We'll write a function that determines the length of a string in a bit.

Say we create an array of characters that we intend to use as a string

  char text[100];
We cannot normally do the following:
  text = "Hello World";
In most compilers you will get a compile time error that says "LValue required in function ...". However I hear that a lot of the newer compilers are allowing you to do this. C++ provides a function in string.h called strcpy that copies one string to another. You can use this function to assign a value to your array of characters. Here's a complete program:
  #include < string.h >
  #include < iostream.h >

  main() {
    char text[100];
    strcpy(text, "Hello World");
    cout << text << endl;
    
    return 0;
  }
First we include the header files we'll need in this program. We need string.h for the strcpy function and iostream.h for using cout (for printing to the screen). In the main procedure we create an array of characters with 100 characters in it (101 actually, but the last character is the null terminator).

All we are doing in this above program is copying one array ("Hello World", an array of 11 characters) into another (text, an array of 100 blank characters). The strcpy function from string.h does this for us. If you wanted to write some code that does basically the same thing as the strcpy function in the above code example, it could look something like this:

  char destination[100];
  int i;

  char source[] = "Hello World";

  for( i=0; i < strlen(source); i++ ) {
    destination[i] = source[i];
  }
  destination[i] = 0;
We are creating two arrays of characters (strings) in this code. The first one is our destination string. We're allocating 100 characters that can be filled with anything we want. The second string, source, is initialized with the string "Hello World". This means that source[0] = 'H', source[1] = 'e', source[2] = 'l', and so on. The [] in the initialization tells us that we're sizing the array to have just enough characters for the "Hello World" string.

After we've initialized everything, we copy the source string into the destination string. We do this by itterating through each element in the source array. We start i at 0, and go while it's less than the length of the string, which we can get with the string.h function strlen(). We simply assign each element of source to the equivalent element of destination. After the for loop, notice that we assign the ith element of destination to 0. This is our null terminator, and tells the program that the string is done. Even though we have enough room for 100 characters, the compiler does not automatically put a null terminator in the character array unless we initialize it as a string with the "" marks around the text. The value of i after the loop will be one greater than the last character we put into the destination array, so we're at the right place to put a null terminator.

You might be tempted to think of source as a regular variable (like an int or a float) instead of what it actually is: an array of characters. For instance, this code will not work on most compilers:

  destination = source;
The = operator on some newer compilers is defined for strings so you can in fact do this, but just to be safe, don't. Your code will be less portable. For instance, this will give you an error on a UNIX system in most cases.

More to Come!  [under 
construction]


This page was last updated on 7-15-97