上海交通大学交大密西根 联合学院·一 ◆] 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array vs.Primitive Variable You can not use an array name on the left of an assignment because array is not associated with a memory "box"whose value can be changed. An array name is associated with a collection of boxes-array elements-whose values can be accessed and changed individually by specifying #define MAX EXAMS 500 an index. main() int i; int scores [MAX EXAMS]; scores This is not meaningful (and is illegal)
Array vs. Primitive Variable Array vs. Primitive Variable • You can not use an array name on the left of an assignment because array is not associated with a memory “box” whose value can be changed. • An array name is associated with a collection of boxes – array elements – whose values can be accessed and changed individually by specifying an index
上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Quick Reminder We can declare arrays using a square-bracket notation.The size can be specified by an integer or a #defined constant. We reference array elements using square brackets.The indices have #define MAX EXAMS 500 to be integers main() or expressions that evaluate int i; int scores [MAX EXAMS]; to integers. Square brackets and an index are scores[1]99; used to set or access the value of an array element. i=··· scores[i]100; It's up to you to be sure the index is valid. if (scores[i]high) high scores[i];
Quick Reminder Quick Reminder • We can declare arrays using a square-bracket notation. The size can be specified by an integer or a #defined constant. • We reference array elements using square brackets. The indices have to be integers or expressions that evaluate to integers
上海交通大学交大密西根 ·联合学院一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array as a Parameter It is possible Passing Arrays as Parameters to pass an array as an #define N VALUES 500 argument to a main() function.We int i; provide the int values [N VALUES]; name of the ClearIntegerArray(values,N VALUES); array as the argument. We do use an array name by itself when we pass an array as an argument
Array as a Parameter Array as a Parameter • It is possible to pass an array as an argument to a function. We provide the name of the array as the argument
上海交通大学交大密西根 联合学院·一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Example Here is an Passing Arrays as Parameters example. We will #define N VALUES 500 main() write a int i; function int values [N VALUES]; that sets ClearIntegerArray(values,N VALUES); the elements of main the array passed to it 口 to zero. values 0 2 498 499
Example Example • Here is an example. We will write a function that sets the elements of the array passed to it to zero
上海交通大学交大密西根 ·联合学院一 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Example Why can that Passing Arrays as Parameters happen?Is that violate the #define N VALUES 500 main() principle of value int i; passing? int values [N VALUES]; You might think of ClearIntegerArray (values,N VALUES); an array as a copy main of the entire collection of array The stack frame for ClearIntegerArray will elements,but that values not contain a copy of the is not what whole array. happens in C. 498 499
Example Example • Why can that happen? Is that violate the principle of value passing? • You might think of an array as a copy of the entire collection of array elements, but that is not what happens in C