The Proper Use of Header Files

Chapter 9 - Arrays

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Initializing Arrays
There are three techniques for initializing arrays:
  By default when they are created. This applies only to global and static arrays.
  Explicitly when they are created, by supplying constant initializing data.
  During program execution, when you assign or copy data into the array.
You can only use constant data to initialize an array when it is created. If the array elements must receive their values from variables, you must initialize the array by writing explicit statements as part of the program code.
Default Initialization
The ANSI C standard specifies that arrays are either global (defined outside of main( ) and any other function) or static automatic (static, but defined after any opening brace) and will always be initialized to binary zero if no other initialization data is supplied. C initializes numeric arrays to zero. Pointer arrays are initialized to NULL. You can run the following program to make certain that a compiler meets this standard:
/*
*   initar.c
*   A C program verifying array initialization
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

#define iGLOBAL_ARRAY_SIZE 10
#define iSTATIC_ARRAY_SIZE 20

int iglobal_array[iGLOBAL_ARRAY_SIZE];          /
*a global array*/

main( )
{
 static int istatic_array[iSTATIC_ARRAY_SIZE]; /
*a static array*/
 printf(“iglobal_array[0]: %d\n”,iglobal_array[0]);
 printf(“istatic_array[0]: %d\n”,istatic_array[0]);

 return(0);
}
When the program is run, you should see zeroes printed verifying that both array types are automatically initialized. This program also highlights another very important point: that the first subscript for all arrays in C is zero. Unlike other languages, there is no way to make a C program think that the first subscript is 1. If you are wondering why, remember that one of C’s strengths is its close link to assembly language. In assembly language, the first element in a table is always at the zeroth offset.
Explicit Initialization
Just as you can define and initialize variables of type int, char, float, double, and so on, you can also initialize arrays. The ANSI C standard lets you supply initialization values for any array, global or otherwise, defined anywhere in a program. The following code segment illustrates how to define and initialize four arrays:
int iarray[3] = {-1,0,1};
static float fpercent[4] = {1.141579,0.75,55E0,-.33E1};
static int idecimal[3] = {0,1,2,3,4,5,6,7,8,9};
char cvowels[] = {‘A’,’a’,’E’,’e’,’I’,’i’,’O’,’o’,’U’,’u’};
The first line of code declares the iarray array to be three integers and provides the values of the elements in curly braces, separated by commas. As usual, a semicolon ends the statement. The effect of this is that after the compiled program loads into the memory of the computer, the reserved space for the iarray array will already contain the initial values, so they won’t need assignments when the program executes. It is important to realize that this is more than just a convenience—it happens at a different time. If the program goes on to change the values of the iarray array, they stay changed. Many compilers permit you to initialize arrays only if they are global or static, as in the second line of code. This statement initializes the array fpercent when the entire program loads.
The third line of code illustrates putting the wrong count in the array declaration. Many compilers consider this an error, while others reserve enough space to hold whichever is greater—the number of values you ask for or the number of values you provide. This example will draw complaints from the Visual C/C++ compiler by way of an error message indicating too many initializers. In the opposite case, when you ask for more space than you provide values for, the values go into the beginning of the array and the extra elements become zeros. This also means that you do not need to count the values when you provide all of them. If the count is empty, as in the fourth line of code, the number of values determines the size of the array.
Unsized Initialization
You can provide the size of the array or the list of actual array values. It usually doesn’t matter for most compilers, as long as you provide at least one of them. For example, a program will frequently want to define its own set of error messages. This can be done two ways. Here is the first method:
char szInput_Error[37] = “Please enter a value between 0 - 9:\n”;
char szDevice_Error[16] = “Disk not ready\n”;
char szMonitor_Error[32] = “Program needs a color monitor.\n”;
char szWarning[44]="This operation will erase the active file!\n";
This method requires you to count the number of characters in the string, remembering to add 1 to the count for the unseen null-string terminator \0. This can become a very tedious approach at best, straining the eyes as you count the number of characters, and very error prone. The second method allows C to automatically dimension the arrays through the use of unsized arrays, as shown here:
char szInput_Error[] = “Please enter a value between 0 - 9:\n”;
char szDevice_Error[] = “Disk not ready\n”;
char szMonitor_Error[] = “Program needs a color monitor.\n”;
char szWarning[] = “This operation will erase the active file!\n”;
When an array initialization statement is encountered and the array size is not specified, the compiler automatically creates an array big enough to hold all of the specified data.
There are a few major pitfalls that await the inexperienced programmer when initializing arrays. For example, an array with an empty size declaration and no list of values has a null length. If there are any data declarations after the array, then the name of the null array refers to the same address, and storing values in the null array puts them in addresses allocated to other variables.
Unsized array initializations are not restricted to one-dimensional arrays. For multidimensional arrays, you must specify all but the leftmost dimension for C to properly index the array. With this approach you can build tables of varying lengths, with the compiler automatically allocating enough storage.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net