Example - More Arrays and Pointers

The following program will create two arrays, fill them, and then
concatentate them into a new array.

#include <iostream.h>

// This is a function prototype.  It lets the compiler know that there
// will be a function of this form.  That way you could refer to the
// function before you have defined it (similar to a forward in Pascal)

int * concat(int  A[], int sizeA, int B[], int sizeB);
 

//  Concatenate two arrays into one larger one.

//  We want to return an array.  Therefore our return type is
//            int *     for an array of integers

int *
concat(int  A[], int sizeA, int B[], int sizeB)
{
    int *C; int i;

    //  The new array needs to be as large as the other two combined
    C = new [ sizeA + sizeB ];

    // Copy the first array into the front
    for (i = 0; i < sizeA; i++)
          C[ i ] = A [ i ];

    //  We could use a for loop here too, but this is to demonstrate
    // that you can do the same thing with a while loop
    i = sizeA;
    while ( i < (sizeA + sizeB))
    // Copy the second array into the new array
    {
        C[ i ] = B[ i - sizeA ];
          i++;
    }

    // Return the new array
    return C;
}

main()
{
     int *A, *B,  *C, i;

    // Create two arrays
    A = new [ 5 ];
    B = new [ 5 ];

    // Fill them up
     for (i = 0; i < 5; i++)
      {
            A[ i ] = i;
            B[ i ] = i + 5;
        }

    // Concatenate them
    C = concat( A, 5, B, 5 );

    for (i = 0; i < 10; i++)
        cout << C[ i ] << " ";
    cout << endl;
}

This program will produce the output:

0 1 2 3 4 5 6 7 8 9
 
 

Structures

Structures are similar to records in Pascal.  A typical definition of a structure follows.

struct Student
{
        char Firstname[ 12 ];
        char LastName[ 20 ];
        int StudentNum;
        float GradePointAvg;
};

In this case our structure is named Student.  It has four internal elements, two of which
are arrays of characters.  In C and C++ there are no common string types (some versions
have their own strings built in, but there is no standard).  Instead, strings are implemented
as arrays of characters (denoted by the type char).  We will examine strings in more detail
later on.  The other new data type introduced in this structure is float.  A float is roughly
equivelent to the real type in Pascal.

We can now define variables of type Student as follows:

Student S;

Elements of the structure are accessed by the . operator as follows.

S.GradePointAvg = 4.0;

Next week we will learn about classes, which greatly extend the basic C concept
of structures, but for now we will stick to structures in order to develop some other
constructs first.  Remember from the discussion of parameter passing, that parameters
are passed by value in C++.  As you might imagine, with a large structure this can
occur significant overhead in a program.  An alternative is simply to pass by
reference. Eg.

void PrintInfo( Student & TheStudent );

This has its own problems.  Namely, sometimes you do not want the function you
are passing the structure to, to have the ability to make changes to the elements
within the structure.  Fortunately there is a solution to this problem too, the
constant reference.  An example follows:

void PrintInfo( const Student & TheStudent );

The const declaration means that the function will have the reference value, but
cannot make changes to internal elements.  This makes software management
much simpler.  It buys you more efficiency than simple pass-by-value, without
having to worry about side effects.  This might seem silly, since when you write
the function you can control the side effects yourself, but many pieces of software,
including some we will write in this class, are produced by teams of people who
work on different parts.  Mechanisms such as these make such projects more
efficient.  The above statements also reflect another software engineering device
that is used in C++ -  the function prototype.  Both of the statements above
define a function PrintInfo which has no body.  Definitions of this type are
called prototypes.  The prototype declaration lets the compiler know to expect
to see that function fully defined later, and that calls to the function, which can
occur before the definition, are ok.  Some compilers, such as CodeWarrior
will actually provide a warning if you try to define a function without first providing
a prototype.  Prototypes are typically put into header file (the .h files).
 

More function management - Scope

The scope of a variable is the collection of places where it is defined.  For the
most part, scope in C is relatively easy to understand.  Generally the scope of
a variable is the function in which it is declared.  Such a variable is called
a local variable.  Local variables have several important properties: It is possible, however, to define variables with larger scope.  If you define a variable outside any function,
at the top of a file, then its scope will be all of the functions within that file.  Such
a variable, called a global variable has the following properties: