CSC 101 Lecture Notes Week 7, Part 3
CSC 101 Lecture Notes Week 7, Part 3
A Couple Details about Arrays
-
Aggregate array initialization (textbook pp. 601-602 and 671-672).
-
Although arrays cannot be assigned to as aggregates, they can be initialized in
a declaration as such.
-
For example, the following declaration initializes the array a to
contain the elements 1 through 5.
int a[] = {1, 2, 3, 4, 5};
-
Note that the array size value that normally goes between the square brackets
is optional when an initializing value is given.
-
Declaring array parameters (pp. 608-610).
-
When an array is declared as a function parameter, the size value is
optional.
-
When the size of an array parameter is omitted, then the function can be called
with arrays of any size.
-
E.g., the following function can be called with an integer array of any size:
void DoSomethingWithAnArray(int a[]);
-
Summary of rules for declaring arrays.
-
An uninitialized array variable must always have a size.
-
The size is optional in an initialized array variable.
-
The size is optional in an array parameter.
-
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