CIS3355:
Business Data Structures |
What types of arrays declarations are there and what do they mean?
The C programming language allows for three types of array declaration First of all let’s understand the definition of array; An array, also known as a vector or list, is one of the simplest data structures in computer programming. Arrays hold a fixed number of equally sized data elements, generally of the same data type. Individual elements are accessed by index using a consecutive range of integers, as opposed to an associative array. Some arrays are multi-dimensional, meaning they are indexed by a fixed number of integers, for example by a tuple (a finite sequence of objects) of two integers; one- and two-dimensional arrays are the most common. A one dimensional array is referred to as a vector. Multi-dimensional arrays are referred to as Matrices (pl.) or a Matrix (sg.). Ok, so know we know what an array is, now let’s learned what the three ways of declaring arrays are. #1 Array Declaration – Automatic Array: (Local Arrays) · Defined within the function · Local to the function in which declared (i.e., references to the array from other arrays cannot be made). · Exist only during the existence of the function · Not initialized Automatic Arrays, also know as Local Arrays, are the most common arrays declaration. Automatic arrays are local arrays whose sizes depend upon values associated with dummy arguments. Automatic arrays are automatically created (allocated) upon entry to the procedure and automatically deallocated upon exit from the procedure. The size of an automatic array typically. An Example of automatic array is: int anadditionalfunction (int *array2); // function prototype void main ( ) {int array1 [10]; // array1 is an automatic array . . . anadditionalfunction (array1); // pass array1 to array2 . . . } int anadditionalfunction (int *array2) // the external function { . . . }
In this example, both array1 and array2 are automatic (local) variables. Variable array1 cannot be referred to from function anadditionalfunction (even though we passed the address of array1 to array2, which duplicated the values into array2). Similarly, we cannot refer to array1 from function anadditionalfunction. Because we are passing the address of the array (array1 in function main and array2 in function anadditionalfunction) we are calling by reference How would this appear in RAM??? Assume that the base address of the array (in function main, array1) is 78567 (REMEMBER: array1 == &array1[0] == 78567). That means that the array takes up locations 78567 through 78586 (since we require 2 * 10 = 20 contiguous bytes of storage). There is one additional address we must consider in the program: array2, which is a location in RAM which will contain an address at which we expect to find an integer (on 2-bytes). Let’s assume that array2 is assigned the address 78591 (through 78594). If that is true, then the relevant portion of RAM (AFTER the call to anadditionalfunction) would appear as (Figure 4.10):
Notice that the only real piece of information that we pass (to function anadditionalfunction) is the address of array1. However, as we have stated repeatedly, is that ALL we need to know is: 1. An address in RAM 2. What type of data is stored at that address Once we know that information, we can determine the information stored there (just as we have been emphasizing all along). # 2 Array Declaration – External Array: (Global Arrays) · Defined outside the function · Known to ALL functions · Do NOT expire (as long as the program is running) · Initialized when declared (set to 0 (zero) if numeric; null if character) For example, consider the C/C++ Code: int array[10]; // array is a GLOBAL array (available to ALL // functions in the program) int anadditionalfunction ( ); // function prototype void main ( ) { . . . anadditionalfunction ( ); // call to the external function . . . anadditionalfunction ( ); . . . } int anadditionalfunction ( ) // the external function { . . . }
In this case variable array could be reference from any function (including function main). We could refer to variable array from either main or from function anadditionalfunction, and access any element in the array (variable array). Note that declaring additional variable from within either of the functions (functions main or anadditionalfunction, as we did with array1 and array2 in the previous example) does not mean that we could make reference to them from the other functions; they would still remain as automatic (local) variables. # 3 Array Declaration - Static Arrays: (Static arrays) · Like automatic arrays, local to the function which declared them · Like external arrays, retain values between calls · Like external arrays, initialized at the time of declaration (set to 0 (zero) if numeric; null if character) What’s the difference between static and automatic arrays??? If we had made the declaration (in function anadditionalfunction) int array2[10]; (automatic) instead of static int array2[10]; any values we established for array2 after the first pass would have disappeared when we made the second call to the function. Notice, once again, that since array1 is an automatic array (i.e., local to function main only) we could not make reference to it from function anadditionalfunction. Key Points to Remember · Arrays may be of storage class automatic, external, or static, but not register. · Automatics, external and static arrays can be initialized
·
Array Initialization:
when a list of initializers is shorter than the number of array elements to be
initialized, the remaining elements are initialized to zero, e.g., · If an external or static array is not initialized explicitly, then the system initializes all elements to zero by default. In contrast, automatic arrays are not necessarily initialzied by the system
·
If an array is declared
without a size and is initialized to a series of values, it is implicitly given
the size of initialziers, i.e.,
Good References: 1. A TUTORIAL ON POINTERS AND ARRAYS IN C http://pweb.netcom.com/~tjensen/ptr/pointers.htm 2. ARRAYS http://www.ee.ucl.ac.uk/~mflanaga/c_arrays.html 3. THE DESIGN OF C http://www1.cs.columbia.edu/~sedwards/classes/2003/w4115/langdesign.pdf 4. POINTERS USED TO ACCES ARRAYS http://www.pa.msu.edu/courses/1996spring/PHY405/lect3b.html
Mini-Quiz 1. What is the main advantage of an array? The address of any element in the array can be quickly calculated. 2. What is the main disadvantage of an array? We must allocate CONTIGUOUS storage in RAM and all of the elements MUST be of the same data type 3. What are the major differences between automatic, static, and external arrays?
|