13. Structures, Unions, and Miscellaneous Items

Chapter 9 - Arrays

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

Array Declarations
The following are examples of array declarations:
int  iarray[12];  /* an array of twelve integers    */
char carray[20];  /* an array of twenty characters  */
As is true with all data declarations, an array’s declaration begins with its data type, followed by a valid array name and a pair of matching square brackets enclosing a constant expression. The constant expression defines the size of the array. It is illegal to use a variable name inside the square brackets. For this reason, it is not possible to avoid specifying the array size until the program executes. The expression must reduce to a constant value so that the compiler knows exactly how much storage space to reserve for the array.
It is best to use defined constants to specify the size of the array:
#define iARRAY_MAX 20
#define fARRAY_MAX 15

int iarray[iARRAY_MAX];
float farray[fARRAY_MAX];
Use of defined constants guarantees that subsequent references to the array will not exceed the defined array size. For example, it is very common to use a for loop to access array elements:
#include <stdio.h>

#define iARRAY_MAX 20

int iarray[iARRAY_MAX];

main( )
{
 int i;
 for(i = 0; i < iARRAY_MAX; i++) {
 .
 .
 .
 }
 return(0);
}

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