![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
The only statement within the body of the function is an expression, multiplying the two arguments together to yield the result. The result of the multiplication is assigned to vals.result through the reference argument result. Notice that the dereference operator is not required with references (to access the object referred to). You do not have to use the dereference operator because dereferencing happens automatically behind the scenes. This allows a more natural notation. For example, this *ptrValue = result ; // assign through pointer is not as intuitive as refValue = result ; // assign through reference The last two lines in the while loop display the operands of the multiplication and the result. The loop will continue until vals.value1 and vals.value2 equal 0. Beyond the while loops body, the next line of code creates a variable of type int named iVal. On the next line, a second variable is created: an array of char named message. An array is an aggregation of a single data type. You could, for example, create an array of 12 ints, each individual element representing the number of days in each month. In this program, message is a variable that consists of MAX_LEN (20) contiguous chars. Strings in C++ are represented as an array of char, terminated by the null char (hex 0x00). A How-To later in the book will introduce the C++ string class. The string class is an optional and more efficient way to create and use strings in C++. After the two variable declarations, a for loop is used to assign a value to each element of the char array. The first offset in an array is 0, not 1. This is a source of error for many C++ novices; it is referred to as the one-off error. (The problem actually occurs at the end of the array; most developers will access the element one beyond the end of the array.) Take a look at the initialization section of the for loop; iVal is initialized to 0. The iVal variable serves three purposes. First, it is used as an incrementing variable; it starts at 0 and counts up to MAX_LEN. Second, it is used as a subscripting (or indexing) value to access the individual elements of the array. Brackets are used as a notation for subscripting the individual elements of an array. A variable can be used as the indexing value. This is in contrast to the declaration of an array; the value that specifies the number of elements in an array must be a constant. The third functionality of iVal is to create a unique letter of the alphabet to be stored in each element. The for loops statement message[ iVal ] = A + iVal ; contains the expression A + iVal The result of this expression is the character (constant) A added to the value of iVal. After each pass through the loop, the result of the expression will be the next letter in the alphabet. So, as soon as the loop terminates, the array message will contain the first 20 letters of the alphabet. The statement after the for loop assigns the null character to the last array element. The next line of code, the cout statement, prints out the contents of the array. The output from this statement appears as follows: Contents of message[ABCDEFGHIJKLMNOPQRST] Lets look at the next three lines of code in the program: char *pc = message ; for( iVal = 0; iVal < MAX_LEN; iVal++, pc++ ) *pc = \x00 ; The first line defines a pointer to a character and is initialized to point to the array message. Initially, pc points to the first element of message. This for loop is used to set each element of the array to the null character. The variable iVal is used as the incrementing variable, counting up to MAX_LEN. Notice that the update expression of the for loop contains two expressions. Both iVal and pc are incremented by 1. This facilitates the pointer pc visiting each element of the array in turn. The for loops statement *pc = \x00 ; uses the dereference operator to assign the null character to the location currently pointed to by pc. The line of code after the for loop prints out the contents of the array. You should see this: Contents of message[] Notice that no letters are printed between the brackets. This is because cout, when printing an array of char, stops printing when it sees the null character. Because the previous for loop set each element of the array to the null character, there is nothing to print. The last line of code in mains body is the return statement, effectively ending the program. Comments Pointers and references are very powerful mechanisms. They provide you with the ability to access objects that reside somewhere in memory. You can think of pointers and references as proxy objects. Be cautious with pointer use; pointers are the root cause of many headaches. Wild pointers have brought down many good applications. Invalid pointers can be hard to detect because the path through a program is usually never the same for each invocation. Use references instead of pointers whenever possible. In fact, you should always choose references over pointers. Arrays are a convenient feature to aggregate like items into one place. Arrays and pointers are usually used together, with pointers being used to access the elements of an array. Accessing an array is much faster using a pointer than using array-offset notation. 1.6 Create data when the program is running?Problem I know how to create global and local variables, but at times I dont know exactly how many variables I will need until runtime. Is there a way to dynamically create simple variables and derived types at runtime?
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|