lecture 6-array

# lecture 6-array - Arrays and Strings 1 Arrays An array is a...

This preview shows pages 1–13. Sign up to view the full content.

1 Arrays and Strings

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

View Full Document
2 An array is a collection of variables of the same type that are referred to through a common name . It is a Data Structure which can hold a set of data items of the same type (e.g., all the exam scores(integers) from test #1. In C, all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Arrays
3 Single-Dimension Arrays General form : type array_name[size]; The amount of storage required: total bytes = sizeof(type) × size of the array.

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

View Full Document
4 SIZE - must be positive constant integral expression: int x[max], y[max*2]; x y 0 0 1 1 9 19 . . . . . . . .
5 For(i=0;i<=4;i++) a[i] = 7 + i* i; a 0 7 + 0 * 0 1 7 + 1 * 1 2 7 + 2 * 2 3 7 + 3 * 3 4 7 + 4 * 4 7 11 8 16 23 An element is accessed by subscripting the array name . This is done by placing the subscript of the element within square brackets after the name of the array. . .

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

View Full Document
6 C has NO subscript checking on arrays . int count[10], i; /* this causes count to be overrun */ for(i=0; i<100; i++) count[i] = i; Memory will be accessed that has garbage” values .
7 If you use an invalid subscript in an expression you will get unpredictable results: Total = count[25] * 24/6 + 9; There isn’t any count[25] array element that we have initialized. But it will be evaluated into a memory address and will use whatever value is represented in those bytes.

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

View Full Document
8 On the other hand if you use an invalid subscript in an assignment , you will destroy some undetermined portion of your program . Usually, but not always, your program will continue to run and either produce unpredictable results or abort. Count[50] = total * taxrate – penalty; Count[50] is an address that may be within The memory allocated for your program.
9 1-D Arrays Initialization General form type array_name[size] = {value_list}; int scores[5] = {60, 40, 90, 80, 60}; scores 0 1 2 3 4 60 90 80 60 40

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

View Full Document
10 1-D Unsized Array Initializations If the size of the array is not explicitly specified , the compiler automatically creates an array big enough to hold all the initializers present. This is called an unsized array . char e2[] = "Write error\n"; printf("%s has length %d\n",e2, sizeof (e2) ) ; Size would be 13 (“\n” 1 char, plus the Null char “/0” 1 char.
11 Variable-Length Arrays You can declare an array whose dimensions are specified by any valid expression, including those whose value is known only at run time . This is called a variable-length array . Only local arrays . The Array “str” will be set as large as the argument passed to “dim”. void f(int dim ) { char str[ dim ]; }

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

View Full Document
12 The fact that an: Array name is a memory address is Exploited in C.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 54

lecture 6-array - Arrays and Strings 1 Arrays An array is a...

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

View Full Document
Ask a homework question - tutors are online