CMPT 125 Lecture 2: L2 P2
Document Summary
The variable array is really a pointer to int. The elements are stored in the memory in a contiguous block, starting from the position array. Arithmetics of pointers: note that size of int = 4. *(array+3) = 25; printf("array[3] = %d\n", array[3]); // array[3] = 25; Array bounds int array[10] = {0, 1, 8, 2, 18, 3, 6, 2, 2, -4}; *common bug for (int i=0; i < length; i++) ; { Do something array[i]; for (int i=0; i < length , i++) { It is possible to have several commands in the increment part, separated by commas. for (int i=0; i < length; i++) a single instruction array[i]; If the body of the loop contains only one instructions, the curly braces can be omitted. (don"t do it!) Exercises: write a function that gets two arrays of ints of length n, and compares them. If the contents of the arrays are identical, it returns -1.