A First Look at the C Class

Chapter 9 - Arrays

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

Calculating Array Dimensions
You have already learned that the sizeof( ) operator returns the physical size, in bytes, of the data object to which it is applied. You can use it with any type of data object except bit-fields. A frequent use of sizeof( ) is to determine the physical size of a variable when the size of the variable’s data type can vary from machine to machine. You have already seen how an integer can be either 2 or 4 bytes, depending on the machine being used. If an additional amount of memory to hold seven integers will be requested from the operating system, some way is needed to determine whether 14 bytes (7 2 bytes/integer) or 28 bytes (7 4 bytes/integer) are needed. The following program automatically takes this into consideration (and prints a value of 28 for systems allocating 4 bytes per integer cell):
/*
*   sizeof.c
*   A C program applying sizeof( ) to determine an array’s size
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

#define iDAYS_OF_WEEK 7

main( )
{
 int ivideo_library[iDAYS_OF_WEEK]={1,2,3,4,5,6,7};

 printf(“There are %d number of bytes in the array”
   “ ivideo_library.\n”,(int)sizeof(ivideo_library));

 return(0);
}
This concept becomes essential when the program must be portable and independent of any particular hardware. If you are wondering why there is an int type cast on the result returned by sizeof( ), in the ANSI C standard sizeof( ) does not return an int type. Instead, sizeof( ) returns a data type, size_t, that is large enough to hold the return value. The ANSI C standard added this to C because on certain computers an integer is not big enough to represent the size of all data items. In the example, casting the return value to an integer allows it to match the %d conversion character of the printf( ) function. Otherwise, if the returned value had been larger than an integer, the printf( ) function would not have worked properly.
By changing iarray‘s data type in the following program, you can explore how various data types are stored internally:
/*
*   array.c
*   A C program illustrating contiguous array storage
*   Copyright (c) Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

#define iDAYS 7

main( )
{
 int index, iarray[iDAYS];

 printf(“sizeof(int) is %d\n\n”, (int)sizeof(int));

 for(index = 0; index < iDAYS; index++)
   printf(“&iarray[%d] = %X\n”, index,
           &iarray[index]);

 return(0);
}
If the program is run on a machine with a word length of 2 bytes, the output will look similar to the following:
sizeof(int) is 4

&iarray[0] = 64FDDC
&iarray[1] = 64FDE0
&iarray[2] = 64FDE4
&iarray[3] = 64FDE8
&iarray[4] = 64FDEC
&iarray[5] = 64FDF0
&iarray[6] = 64FDF4
Notice how the & (address) operator can be applied to any variable, including an array element. An array element can be treated like any other variable; its value can form an expression, it can be assigned a value, and it can be passed as an argument (or parameter) to a function. In this example you can see how the array elements’ addresses are exactly 2 bytes apart. You will see the importance of this contiguous storage when you use arrays in conjunction with pointer variables.
The following C++ listing is similar in structure to the program just discussed:
//
//  array.cpp
//  A C++ program illustrating contiguous array storage
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>

#define iMAX 10

main( )
{
 int index, iarray[iMAX];

 cout << “sizeof(int) is ” << (int)sizeof(int) << “\n\n”;

 for(index = 0; index < iMAX; index++)
   cout << “&iarray[” << index << “] = ” << index
        << &iarray[index] << endl;

 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