In order to intialize an array you simply declare it as follows: int newArray [10]; (type name [ size ];) This creates an array of integers and declares the name and size. There are many ways to fill an array. An example would be int newArray [3] = { 1, 2, 3,}. Arrays can consist of any data type and can also include records, meaning multiple fields per item. (this will be discussed in future posts.) Array data is accessed by looking at a memory location. (newArray[2];) Assuming we have an array of integers 1,2,3 array location [2] is equal to the integer 3. The first memory location in the array is referenced by newArray[0]. The following code is an example of initializing an array and using a for loop to print out the values of the entire array.

In the next post expect to see more about arrays and multidimensional arrays.
No comments:
Post a Comment