GO
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


1.5 Use the derived data types such as arrays and pointers?

Problem

I have written a few C++ programs now and feel very confident. I built programs to take advantage of loops, simple variables, user input and message output, and user defined functions. I need to now explore the use of more advanced data types such as arrays, structures, and pointers.

Technique

If you have an understanding of C++’s basic data types, learning to use the advanced types will come easy. The technique for using derived types is no different from using plain ol’ data (POD) types. Most of the derived types are just aggregates of the PODs. This How-To will also demonstrate the use of pointers and references. As usual, you will learn about these advanced types from the source code that follows.

Steps

1.  Start at you base source directory and create a new subdirectory named TYPES.
2.  As usual, fire up your favorite text editor and type in the following source code:
// types.cpp - program to demonstrate the
// advanced types used in C++
#include <iostream>
using namespace std ;
void multiply( long &left, long &right, long &result ) ;
void getValue( const char *message, long *value ) ;

struct values {
    long value1 ;
    long value2 ;
    long result ;
} ;
const int MAX_LEN = 20 ;

int main()
{
    values vals ;

    cout << “\nProgram to multiply two integers “ ;
    cout << “and display the result. “ << endl ;
    cout << “It will also show the contents of an array” << endl ;

    vals.value1 = vals.value2 = vals.result = 1L ;
    while( vals.result != 0 )
    {
        vals.value1 = vals.value2 = vals.result = 0 ;

        cout << “\nTwo zeros will end the program.” << endl ;

        getValue( “Enter the first integer: “, &vals.value1 ) ;
        getValue( “Enter the second integer: “, &vals.value2 ) ;

        if( vals.value1 == 0 && vals.value2 == 0 )
            break ;

        multiply( vals.value1, vals.value2, vals.result ) ;
        cout << vals.value1 << “ multiplied by “ ;
        cout << vals.value2 << “ = “ << vals.result” << endl ;
    }

    int iVal ;
    char message[ MAX_LEN +1 ] ;

    for( iVal = 0; iVal < MAX_LEN; iVal++ )
        message[ iVal ] = ‘A’ + iVal ;
    message[ iVal ] = ‘\x00’ ;

    cout << “\nContents of message[“ << message << ‘]’ << endl ;

    char *pc = message ;
    for( iVal = 0; iVal < MAX_LEN; iVal++, pc++ )
        *pc = ‘\x00’ ;

    cout << “\nContents of message[“ << message << ‘]’ << endl ;

    return 0 ;
}

// multiply two numbers and put result in third argument
void multiply( long &left, long &right, long &result )
{
    result = left * right ;
}
// display message, get value
void getValue( const char *message, long *value )
{
    cout << message ;

    long result = 0 ;
    cin >> result ;
    *value = result ;
}
3.  Save your work in a file named TYPES.CPP and exit the editor back to the command line.
4.  Run the compiler and linker on the source file.
5.  Now, run the program. The output should be similar to the following:
Program to multiply two integers and display the result.
It will also show the contents of an array

Two zeros will end the program.
Enter the first integer: 4
Enter the second integer: 4
4 multiplied by 4 = 16

Two zeros will end the program.
Enter the first integer: 0
Enter the second integer: 0

Some letters: ABCDEFGHIJKLMNOPQRST
Contents of message[]

How It Works

Your journey begins with lines 5 and 6:

void multiply( long &left, long &right, long &result ) ;
void getValue( const char *message, long *value ) ;

These function declarations look similar to the declaration in How-To 1.4. The first declaration accepts three arguments, each of which is a reference to a long. The second declaration accepts two arguments that are pointers.

The function multiply has three arguments that are references to long. A reference is exactly what its name implies—it refers to some other object. The reference itself is not the object of interest; the object of interest is accessed using the reference.

The getValue function owns two arguments. One is a pointer to a const char and the second is a pointer to long. A pointer, as its name implies, refers (or points) to some other object. A pointer, as with a reference, is not the object of interest; the object of interest is accessed using the pointer. The argument value is a variable, just as you’ve seen in previous How-Tos; it’s just that this variable happens to be pointing to some object (variable) that exists somewhere else in the program. Do not concern yourself with the contents of the pointer itself. You should only be interested in the object to which the pointer points.

You might be asking, “If a reference and a pointer both refer to some other object, what is the difference between the two?” This is a common question and deserves an answer. I mentioned that a pointer is a variable (in the sense that its contents can change), but I did not imply the same for references. Therefore, a difference is that you can change what the pointer points to. With a reference, you cannot do this; a reference must refer to some object. After the reference refers to an object, you cannot change the reference to refer to another object. This implies that a reference always refers to a single object. Do not let me mislead you, however: A pointer can be constant, which means that it can only point to a single object. The following declaration shows this:

char theChar = ‘B’ ;
char * const pconst = &theChar ;

This declaration states that pconst is a constant pointer to a char—namely, theChar. Although pconst can change the value stored in theChar, pconst cannot point to anything except theChar.

So, what do a reference and a pointer store as their value? Quite simply, they store the address of the object that they refer to. This is how a reference and a pointer gain access to an object. Think of it this way: Every house (or office, and so on) has an address. In your address book, you have the address written down for some friend. The address in your address book is not the actual house, but is simply a reference to the location of the house. You first look up the address and then use it to locate and access the house.

Beginning on line 8, a struct declaration is introduced:

struct values {
    long value1 ;
    long value2 ;
    long result ;
} ;


Previous Table of Contents Next


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.