上海交通大学交大密西根 B ·联合学院一 1811 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Element Accessing Besides being used in declaration,square brackets are used to refer to particular members of arrays in assignment statements or in expressions. Arrays Array elements are referenced in expressions of the form array-name[index] main ( main char letters[4]; letters int values[3]; A 25 int i; 0123 letters[0]A'; values va1ues[1]=23; 23 0 1 i values[1]2;
Array Element Accessing Array Element Accessing • Besides being used in declaration, square brackets are used to refer to particular members of arrays in assignment statements or in expressions
上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Element Accessing Since an array element is something that can go on the left side of an assignment,we can use operators like++ with an array element. value[1]++; Arrays is just shorthand for Array elements are referenced in expressions of the form value[1]+1; array-name[index] main ( main char letters[4]; int values[3]; letters int i; a 25 0123 letters[0]=A'; values va1ues[1]=23; 24 i=values[1]+2; 0 1 values [1]++;
Array Element Accessing Array Element Accessing • Since an array element is something that can go on the left side of an assignment, we can use operators like ++ with an array element. value[1]++; is just shorthand for value[1] += 1;
上海交通大学交大密西根 联合学院·一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Indexing We can use an integer variable or expression as an array index.This is a very typical for loop. Arrays Array elements are referenced in expressions of the form array-name[index] main ( main char letters[4]; letters 1 int values[3]; 3 int i; 0123 for (i=0;i<3; values i++) 0 0 0 values[i]0; 0 2
Array Indexing Array Indexing • We can use an integer variable or expression as an array index. This is a very typical for loop
上海交通大学交大密西根 联合学院·一 ◆ 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Array Size Definition Using a #defined constant as the array size is often convenient in declarations.It is not possible in C to use a variable as the size of an array. #define MAX EXAMS 500 main() int i; int scores [MAX EXAMS]; What goes here must be a constant,because as it compiles your program,the compiler needs to know how much memory to set aside
Array Size Definition Array Size Definition • Using a #defined constant as the array size is often convenient in declarations. It is not possible in C to use a variable as the size of an array
上海交通大学交大密西根 ·联合学院一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Indexing Problem Any integer variable i can be used as an array index.C comnilars do not check to see thet the indey valis V #define MAX EXAMS 500 main() [ W int i; int scores [MAX EXAMS]; al Square brackets and an index are ti scores[1]99; i=·· used to set or access the value of an scores[i]100; array element. if (scores[i]high) It's up to you to be sure the index high scores[i];is valid
Indexing Problem Indexing Problem • Any integer variable i can be used as an array index. C compilers do not check to see that the index value is valid for the given array. If i here was 523, the statement scores[i] = 100; would store 100 somewhere, but it wouldn’t be in the array! Doubtless an error would occur, but maybe not till later