![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
The keyword struct is really shorthand for structure. A struct is considered a user-defined type. A struct is a collection of one or more types. The types (variables) can be PODs, arrays, or other structs. Notice the declarations of the variables within the struct. The declarations are no different from declaring global or local variables. The difference with a struct is that the variable declarations are contained within the struct. The declaration simply defines the layout for the struct; it is not a variable declaration. Consider a struct declaration as a template only; no memory is set aside for a struct declaration. The first statement within the main function declares the variable values of type struct, as shown in the following code: values vals ; This declaration is the same as declaring variables of type int or float; memory is allocated for the object. You can now access the individual member variables of the struct. The dot (membership) operator (.) is used to access the member variables of a struct through a struct variable. For example, vals.value1 = 0 ; assigns the value 0 to the member variable value1 for the object vals. You can initialize a struct variable at the time of declaration also, just as you can with simple (POD) variables. You specify an appropriate initialization value for each member variable, separating each value with a comma. The initializations are enclosed within a pair of braces. The following snippet of code demonstrates this: values someValues = { 10L, 20L, 30L } ; This statement creates a variable of type struct values named someValues. The three elements of values are each initialized to the values 10L, 20L, and 30L, respectively. The postfix L on each number says that these constants are long. The next three lines of code send a message to the standard output describing the intent of the program. The next line of code vals.value1 = vals.value2 = vals.result = 1L ; initializes the individual members of the variable vals to the value 1L. You could initialize vals using structure initialization as demonstrated above. The next line of code, a while loop, evaluates the variable vals.result for a non-zero value. If the value becomes 0, the loop terminates. The first line of code inside the loop vals.value1 = vals.value2 = vals.result = 0 ; initializes the member variables of vals to 0. The intent is to put the member variables into a known state. The next line of code displays a message to the user, giving directions on the use of the program. The next two lines of code getValue( Enter the first integer: , &vals.value1 ) ; getValue( Enter the second integer: , &vals.value2 ) ; call the function getValue, with each call passing two arguments to the function. The first argument, a pointer to a const char, is a string of text that is displayed to the user. The second argument is a pointer to a long. Notice that each argument is separated by a comma. The second arguments name is prepended with the address-of operator. Because a pointer must contain an address to be useful, the address-of operator is required to represent the address of the variable. Remember that arguments are passed by value, so the expression &vals.value1 yields the address of the variable. Because the argument is a copy of the actual object, and the object is the address of the variable, the argument is the object expected: a pointer to a long. Lets examine the getValue function. // display message, get value void getValue( const char *message, long *value ) { cout << message ; long result = 0 ; cin >> result ; *value = result ; } The first argument means message is a pointer to a const char. Effectively, you can change what the pointer points to, but you cannot change the contents to which the pointer points. The second argument is a pointer to a long; you can alter the contents (change the value) to which the pointer points. The first line in the function body displays the contents of message to the screen. The next line creates a local variable of type long named result. This variable is used to capture the required input from the user. The last line in the functions body assigns the value contained in result to the variable to which value points. Notice that the dereference operator (*) is used to perform this magic. The dereference operator is used to access the object through the pointer. Remember that value does not contain a long value; it merely refers to a long variable. Because the pointer value points to vals.value1, it is vals.value1 that is the recipient of the value. Back in the main function, the next call to getValue performs the same functionality, eventually assigning a long value to vals.value2. The if statement checks to see whether both vals.value1 and vals.value2 are equal to 0. If so, the while loop is terminated. The next line of code within the loop multiply( vals.value1, vals.value2, vals.result ) ; calls the function multiply, passing three arguments. In this instance, the target arguments are references to longs. Here is the function again, in its entirety: // multiply two numbers and put result in third argument void multiply( long &left, long &right, long &result ) { result = left * right ; }
|
![]() |
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.
|