chapter08 - COP 3275: Chapter 08 Jonathan C.L. Liu, Ph.D....

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3275: Chapter 08 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Scalar Variables vs. AggregateVariables So far, the only variables we’ve seen are scalar: capable of holding a single data item. C also supports aggregate variables, which can store collections of values. There are two kinds of aggregates in C: arrays and structures. The focus of the chapter is on one-dimensional arrays, which play a much bigger role in C than do multidimensional arrays. 2
Background image of page 2
One-Dimensional Arrays An array is a data structure containing a number of data values, all of which have the same type. These values, known as elements, can be individually selected by their position within the array. The simplest kind of array has just one dimension. The elements of a one-dimensional array a are conceptually arranged one after another in a single row (or column): 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
One-Dimensional Arrays To declare an array, we must specify the type of the array’s elements and the number of elements: int a[10]; The elements may be of any type; the length of the array can be any (integer) constant expression. Using a macro to define the length of an array is an excellent practice: #define N 10 int a[N]; 4
Background image of page 4
Array Subscripting To access an array element, write the array name followed by an integer value in square brackets. This is referred to as subscripting or indexing the array. The elements of an array of length n are indexed from 0 to n – 1. If a is an array of length 10, its elements are designated by a[0] , a[1] , …, a[9] : 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Array Subscripting Expressions of the form a[i] are lvalues, so they can be used in the same way as ordinary variables: a[0] = 1; printf("%d\n", a[5]); ++a[i]; In general, if an array contains elements of type T , then each element of the array is treated as if it were a variable of type T . 6
Background image of page 6
Array Subscripting Many programs contain for loops whose job is to perform some operation on every element in an array. Examples of typical operations on an array a of length N : for (i = 0; i < N; i++) a[i] = 0; /* clears a */ for (i = 0; i < N; i++) scanf("%d", &a[i]); /* reads data into a */ for (i = 0; i < N; i++) sum += a[i]; /* sums the elements of a */ 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Array Subscripting C doesn’t require that subscript bounds be checked; if a subscript goes out of range, the program’s behavior is undefined. A common mistake: forgetting that an array with n elements is indexed from 0 to n – 1, not 1 to n : int a[10], i; for (i = 1; i <= 10; i++) a[i] = 0; With some compilers, this innocent-looking for statement causes an infinite loop. 8
Background image of page 8
Array Subscripting An array subscript may be any integer expression: a[i+j*10] = 0; The expression can even have side effects: i = 0; while (i < N) a[i++] = 0; 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Be careful when an array subscript has a side effect: i = 0; while (i < N) a[i] = b[i++]; The expression a[i] = b[i++] accesses the value of i and also modifies i , causing undefined behavior. The problem can be avoided by removing the
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/30/2011 for the course COP 3275 taught by Professor Jonathanliu during the Fall '11 term at University of Florida.

Page1 / 56

chapter08 - COP 3275: Chapter 08 Jonathan C.L. Liu, Ph.D....

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online