The elements are numbered starting with 0 An array with 8 elements has elements

# The elements are numbered starting with 0 an array

This preview shows page 5 - 9 out of 14 pages.

The elements are numbered starting with 0 An array with 8 elements has elements at 0,1,2,3,4,5,6, and 7 The subscripted variable x[0] (read as x sub zero) refers to the initial or 0th element of the array x, x[1] is the next element in the array, and so on. The integer enclosed in brackets is the array subscript or index and its value must be in the range from zero to one less than the array size.
6 Visual representation of an Array 342901 342905 342909 342913 342917 342921 342925 342929 ? ? 20 ? ? ? ? ? int x[8];  x[2] = 20; 0 1 2 3 4 5 6 7 Memory  Addresses Array  Index/Subscript Array Element Note: Index starts with 0, not with 1
7 Array Initialization When you declare a variable, its value isn’t initialized unless you specify. int sum;// Does not initialize sum int sum = 1;  // Initializes sum to 1 Arrays, like variables, aren’t initialized by default int X[10];  //creates the array, but doesn’t set any of its values. If you have all the values at the point of declaring the array, you can declare and initialize the array at the same time, like: int X[10] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; The array elements are initialized in the order listed X[0] = 2 X[4] = 11
8 Array Initialization … If there are values in the initialization block, but not enough to fill the array, all the elements in the array without values are initialized to 0 in the case of double or int, and NULL in the case of char. int scores[20]  = {0};         // all 20 elements are initialized to 0  int scores[20]  = {1, 2, 3};  // First 3 elements are initialized to 1, 2,             // 3 and the rest are initialized to 0  If there are values in the initialization block, an explicit size for the array does not need to be specified. Only an empty array element is sufficient, C will count the size of the array for you.

#### You've reached the end of your free preview.

Want to read all 14 pages?

• Spring '10
• baleh
• Array, #include, #define, Array Array Initialization