400 likes | 516 Views
C Arrays. Arrays. Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name Position number Format: arrayname [ position number ] First element at position 0 n element array named c: c[ 0 ], c[ 1 ]...c[ n – 1 ].
E N D
Arrays • Array • Group of consecutive memory locations • Same name and type • To refer to an element, specify • Array name • Position number • Format: arrayname[position number] • First element at position 0 • n element array named c: • c[ 0 ], c[ 1 ]...c[ n – 1 ]
Arrays • Array elements are like normal variables c[ 0 ] = 3; printf( "%d", c[ 0 ] ); • Perform operations in subscript. If x equals 3 c[ 5 - 2 ] == c[ 3 ] == c[ x ]
Defining Arrays • When defining arrays, specify • Name • Type of array • Number of elements arrayType arrayName[ numberOfElements ]; • Examples: int c[ 10 ]; float myArray[ 3284 ]; • Defining multiple arrays of same type • Format similar to regular variables • Example: int b[ 100 ], x[ 27 ];
Array Examples • Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; • If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 } • All elements 0 • If too many initializers, a syntax error occurs • C arrays have no bounds checking • If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; • 5 initializers, therefore 5 element array
Outline for loop initializes each array element separately for loop outputs all array elements
Outline initializer list initializes all array elements simultaneously
Common Programming Error 6.2 Forgetting to initialize the elements of anarray whose elements should be initialized.
Common Programming Error 6.3 Providing more initializers in an arrayinitializer list than there are elementsin the array is a syntax error.
Outline #define directive tells compiler to replace all instances of the word SIZE with 10 SIZE= symbolic constant 10=replacement text fig06_05.c (1 of 2 ) SIZE is replaced with 10 by the compiler, so array s has 10 elements for loop initializes each array element separately
Outline initializer list initializes all array elements simultaneously for loop adds each element of the array to variable total
Outline #define directives create symbolic constants frequency array is defined with 11 elements responses array is defined with 40 elements and its elements are initialized subscript of frequency array is given by value in responses array
Outline nested for loop prints n[ i ] asterisks on the ith line
Outline for loop uses one array to track number of times each number is rolled instead of using 6 variables and a switch statement
Array Examples • Character arrays • String “first” is really a static array of characters • Character arrays can be initialized using string literals char string1[] = "first"; • Null character '\0' terminates strings • string1 actually has 6 elements It is equivalent to charstring1[]={'f','i','r','s','t','\0'}; • Can access individual characters string1[3] is character ‘s’ • Array name is address of array, so & not needed for scanf scanf("%s",string2); • Reads characters until whitespace encountered • Be careful not to write past end of array, as it is possible to do so
Outline string2 array is defined with one element for each character, so 15 elements including null character /0 for loop prints characters of string1 array with spaces in between
Outline static array is created only once, when staticArrayInit is first called
Outline automatic array is recreated every time automaticArrayInit is called
Common Programming Error 6.8 Assuming that elements of a local staticarray are initialized to zero every time the function in which the array is defined is called.
Passing Arrays to Functions • Passing arrays • To pass an array argument to a function, specify the name of the array without any brackets int myArray[24]; myFunction(myArray,24); • Array size usually passed to function • Arrays passed call-by-reference • Name of array is address of first element • Function knows where the array is stored • Modifies original memory locations • Passing array elements • Passed by call-by-value • Pass subscripted name (i.e., myArray[3]) to function
Passing Arrays to Functions • Function prototype void modifyArray( int b[], int arraySize ); • Parameter names optional in prototype • int b[] could be written int [] • int arraySize could be simply int
Performance Tip 6.3 Passing arrays by reference makes sense for performance reasons. If arrays were passed by value, a copy of each element would be passed. For large, frequently passed arrays, this would be time consuming and would consume considerable storage for the copies of the arrays.
Software Engineering Observation 6.2 It is possible to pass an array by value
Outline Function prototype indicates function will take an array Array a is passed to modifyArray by passing only its name
Outline Array element is passed to modifyElement by passing a[ 3 ]
Outline const qualifier tells compiler that array cannot be changed Any attempts to modify the array will result in errors