CSC 101 Lecture Notes Week 7, Part 3

CSC 101 Lecture Notes Week 7, Part 3
A Couple Details about Arrays


  1. Aggregate array initialization (textbook pp. 601-602 and 671-672).
    1. Although arrays cannot be assigned to as aggregates, they can be initialized in a declaration as such.
    2. For example, the following declaration initializes the array a to contain the elements 1 through 5.
      
      
      int a[] = {1, 2, 3, 4, 5};
      
    3. Note that the array size value that normally goes between the square brackets is optional when an initializing value is given.

  2. Declaring array parameters (pp. 608-610).
    1. When an array is declared as a function parameter, the size value is optional.
    2. When the size of an array parameter is omitted, then the function can be called with arrays of any size.
    3. E.g., the following function can be called with an integer array of any size:
      
      
      void DoSomethingWithAnArray(int a[]);
      

  3. Summary of rules for declaring arrays.
    1. An uninitialized array variable must always have a size.
    2. The size is optional in an initialized array variable.
    3. The size is optional in an array parameter.
    4. Array parameters are always passed by reference, so an array parameter is never declared using the ampersand '&' symbol.


index | lectures | labs | handouts | assignments | solutions | grades | help