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

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

View Full Document Right Arrow Icon
1 Arrays and Strings
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
3 Single-Dimension Arrays General form : type array_name[size]; The amount of storage required: total bytes = sizeof(type) × size of the array.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 SIZE - must be positive constant integral expression: # define max 10 int x[max], y[max*2]; x y 0 0 1 1 9 19 . . . . . . . .
Background image of page 4
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. . .
Background image of page 5

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

View Full DocumentRight Arrow Icon
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 .
Background image of page 6
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.
Background image of page 7

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

View Full DocumentRight Arrow Icon
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.
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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.
Background image of page 10
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 ]; }
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 The fact that an: Array name is a memory address is Exploited in C. Since a pointer is a memory address, it
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/08/2011 for the course CSCE 206 taught by Professor Hurley during the Spring '11 term at Texas A&M.

Page1 / 54

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 Right Arrow Icon
Ask a homework question - tutors are online